仅仅声明环境变量 openURL 会导致弹出窗口行为不稳定
Merely declaring the environment variable openURL cause popover behavior to go wonky
如以下示例代码所示。当 运行 没有声明 openURL 变量时,弹出窗口正确显示。取消注释这些行,它出现在不正确的位置并拒绝被驳回。我真的可以使用 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
工作正常。
测试 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 变量时,弹出窗口正确显示。取消注释这些行,它出现在不正确的位置并拒绝被驳回。我真的可以使用 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
工作正常。
测试 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()
})
}
}