我有一个自定义的修饰符来处理显示/隐藏键盘,但当在iOS 13上运行时,它不会渲染视图,在一些调查之后,我发现原因是在ViewModifier中使用了GeometryReader。
以下代码将导致空白屏幕:
struct ContentView: View {
var body: some View {
Text("Foo")
.modifier(MyCustomModifier())
}
}
struct MyCustomModifier: ViewModifier {
func body(content: Content) -> some View {
GeometryReader { proxy in
content
// Use proxy
}
}
}这种情况只发生在Xcode13上,而且只有在使用iOS 12编译代码的情况下才会发生。
发布于 2020-10-15 21:03:19
它看起来像是一个bug,所以作为一种解决办法,有两件事可以做:
1.在修饰符外部使用GeometryReader,并将代理传递给修饰符:
struct ContentView: View {
var body: some View {
GeometryReader { proxy in
Text("Foo")
.modifier(MyCustomModifier(geometryProxy: proxy))
}
}
}
struct MyCustomModifier: ViewModifier {
let geometryProxy: GeometryProxy
func body(content: Content) -> some View {
content
// Use proxy
}
}2.只需在GeometryReader中嵌入使用修饰符的视图:
struct ContentView: View {
var body: some View {
GeometryReader { _ in
Text("Foo")
.modifier(MyCustomModifier())
}
}
}
struct MyCustomModifier: ViewModifier {
func body(content: Content) -> some View {
GeometryReader { proxy in
content
// Use proxy
}
}
}发布于 2020-10-30 18:28:29
我遇到了同样的问题,不得不完全放弃使用ViewModifier,转而使用在扩展中使用视图包装的旧方法:
extension View {
/// Conditionally applies bottom padding to the view, if there's no
/// bottom safe area inset (or if it's less than the padding provided).
/// - Parameter padding: The min bottom padding to apply.
@available(iOS, deprecated: 14, message: "Time to switch to the ViewModifier approach.")
func minBottomPadding(_ padding: CGFloat) -> some View {
// Currently there's a bug that prevents using GeometryReader inside ViewModifier on iOS 13.
GeometryReader { geometry in
self.padding(.bottom, geometry.safeAreaInsets.bottom < padding ? padding : 0)
}
}
}谢谢你@奥列格-谢尔曼证实了我的怀疑!
https://stackoverflow.com/questions/64372192
复制相似问题