仅仅声明环境变量 openURL 就会导致弹出窗口行为变得不稳定

2024-02-19

如下面的示例代码所示。运行时弹出窗口会正确显示,而无需声明 openURL 变量。取消注释这些行,它会出现在错误的位置并且拒绝被忽略。我真的可以使用修复/解决方法......

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工作正常。

使用 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()
                 })
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

仅仅声明环境变量 openURL 就会导致弹出窗口行为变得不稳定 的相关文章

随机推荐