iPad Popover中的NavigationView在SwiftUI中无法正常工作 [英] NavigationView in iPad popover does not work properly in SwiftUI
问题描述
我有以下代码,当点击一个按钮时会显示一个弹出框:
struct ContentView: View {
@State private var show = false
var body: some View {
Button("Open") {
self.show.toggle()
}.popover(isPresented: $show, content: {
// NavigationView {
ScrollView {
ForEach(0...10, id: \.self) {_ in
Text("Test popover ...")
}.padding()
}
// }
})
}
}
如果我在弹出窗口的内容中添加NavigationView
,那么我会得到:
知道为什么会这样吗?
如果为内容设置一个固定的框架,效果很好,但是我不想这样做,因为我希望根据其内容调整弹出窗口的大小.
在iPad上,他们可能已经陷入了大小检测的鸡蛋问题,因此只需最小化即可.
无论如何,解决方案是使用预先定义的值(对于iPad来说还不错)或动态计算的(例如,通过GeometryReader
从外框开始)显式设置.frame
这里是一个例子.经过Xcode 12/iPadOS 14的测试
struct TestPopover: View {
@State private var show = false
var body: some View {
GeometryReader { gp in
VStack {
Button("Open") {
self.show.toggle()
}.popover(isPresented: $show, content: {
NavigationView {
ScrollView { // or List
ForEach(0...10, id: \.self) {_ in
Text("Test popover ...")
}.padding()
}
.navigationBarTitle("Test", displayMode: .inline)
}
.frame(width: gp.size.width / 3, height: gp.size.height / 3)
})
}.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
}
变量2:部分根据外部尺寸计算,部分根据内部尺寸计算.
struct TestPopover: View {
@State private var show = false
@State private var popoverWidth = CGFloat(100)
var body: some View {
GeometryReader { gp in
VStack {
Button("Open") {
self.show.toggle()
}.popover(isPresented: $show, content: {
NavigationView {
ScrollView { // or List
ForEach(0...10, id: \.self) {_ in
Text("Test popover ...").fixedSize()
}.padding()
.background(GeometryReader {
Color.clear
.preference(key: ViewWidthKey.self, value: $0.frame(in: .local).size.width)
})
.onPreferenceChange(ViewWidthKey.self) {
self.popoverWidth = $0
}
}
.navigationBarTitle("Test", displayMode: .inline)
}
.frame(width: self.popoverWidth, height: gp.size.height / 3)
})
}.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
}
struct ViewWidthKey: PreferenceKey {
typealias Value = CGFloat
static var defaultValue = CGFloat.zero
static func reduce(value: inout Value, nextValue: () -> Value) {
value += nextValue()
}
}
I have the following code that displays a popover when a button is tapped:
struct ContentView: View {
@State private var show = false
var body: some View {
Button("Open") {
self.show.toggle()
}.popover(isPresented: $show, content: {
// NavigationView {
ScrollView {
ForEach(0...10, id: \.self) {_ in
Text("Test popover ...")
}.padding()
}
// }
})
}
}
If I add a NavigationView
in popover's content then I get this :
Any idea why this happens?
It works fine if I set a fixed frame for the content, but I do not wanna do that since I want the popover to resize according to it's content.
Probably on iPad they've got into chicken-egg problem with size detection, so just finalised with minimum.
Anyway, the solution would be to set .frame
explicitly, either with predefined values (for iPad it is not so bad), or with dynamically calculated (eg. from outer frame via GeometryReader
)
Here is an example. Tested with Xcode 12 / iPadOS 14
struct TestPopover: View {
@State private var show = false
var body: some View {
GeometryReader { gp in
VStack {
Button("Open") {
self.show.toggle()
}.popover(isPresented: $show, content: {
NavigationView {
ScrollView { // or List
ForEach(0...10, id: \.self) {_ in
Text("Test popover ...")
}.padding()
}
.navigationBarTitle("Test", displayMode: .inline)
}
.frame(width: gp.size.width / 3, height: gp.size.height / 3)
})
}.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
}
Variant 2: Partially calculated on outer size, partially on inner size.
struct TestPopover: View {
@State private var show = false
@State private var popoverWidth = CGFloat(100)
var body: some View {
GeometryReader { gp in
VStack {
Button("Open") {
self.show.toggle()
}.popover(isPresented: $show, content: {
NavigationView {
ScrollView { // or List
ForEach(0...10, id: \.self) {_ in
Text("Test popover ...").fixedSize()
}.padding()
.background(GeometryReader {
Color.clear
.preference(key: ViewWidthKey.self, value: $0.frame(in: .local).size.width)
})
.onPreferenceChange(ViewWidthKey.self) {
self.popoverWidth = $0
}
}
.navigationBarTitle("Test", displayMode: .inline)
}
.frame(width: self.popoverWidth, height: gp.size.height / 3)
})
}.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
}
struct ViewWidthKey: PreferenceKey {
typealias Value = CGFloat
static var defaultValue = CGFloat.zero
static func reduce(value: inout Value, nextValue: () -> Value) {
value += nextValue()
}
}
这篇关于iPad Popover中的NavigationView在SwiftUI中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!