仅仅声明环境变量 openURL 会导致弹出行为变得不稳定 [英] Merely declaring the environment variable openURL cause popover behavior to go wonky

查看:66
本文介绍了仅仅声明环境变量 openURL 会导致弹出行为变得不稳定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下面的示例代码所示.在不声明 openURL 变量的情况下运行时,弹出窗口正确显示.取消注释这些行,它出现在不正确的位置并拒绝被驳回.我真的可以使用修复/解决方法...

As demonstrated in the sample code below. The popover appears correctly when run without declaring the openURL variable. Uncomment the lines and it appears in the incorrect spot and refuses to be dismissed. I could really use a fix/work-around...

struct Popover: View {
    @State var isShowingPopover = false
//    @Environment(\.openURL) var openURL

    var body: some View {
        Text("Content")
        .toolbar {
            ToolbarItemGroup(placement: .primaryAction) {
                Button("Popover") {
                    isShowingPopover.toggle()
                }
                .popover(isPresented: $isShowingPopover) {
                    Button(action:{
//                        openURL(URL(string: "https://cnn.com")!)
                    }){
                        Label("Open CNN", systemImage: "hand.thumbsup")
                    }
                        .padding()
                }
            }
        }
    }
}


struct Popover_Previews: PreviewProvider {
    static var previews: some View {
        NavigationView{
            Text("Sidebar")
            Popover()
        }
    }
}

推荐答案

它出现 toolbar 缺陷...使用 navigationBarItems 工作正常.

It appears toolbar defect... using navigationBarItems works fine.

使用 Xcode 12.1/iOS 14.1 测试

Tested with Xcode 12.1 / iOS 14.1

struct PopoverView: View {
    @State var isShowingPopover = false
    @Environment(\.openURL) var openURL

    var body: some View {
        Text("Content")
            .navigationBarItems(trailing:
                 Button("Popover") {
                      isShowingPopover.toggle()
                 }
                 .popover(isPresented: $isShowingPopover) {
                      Button(action:{
                            openURL(URL(string: "https://cnn.com")!)
                      }){
                            Label("Open CNN", systemImage: "hand.thumbsup")
                      }
                            .padding()
                 })
    }
}

这篇关于仅仅声明环境变量 openURL 会导致弹出行为变得不稳定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆