首页 > 解决方案 > 带有表单的 SwiftUI 模态表:设备旋转后左/右填充错误

问题描述

打开包含表单的模式表,然后将设备旋转到横向模式并返回纵向模式时,一切正常。但是,如果模态表在横向模式下打开然后旋转到纵向模式,则左侧和右侧的填充不符合预期。我认为这是 SwiftUI 中的一个错误。有解决方法吗?

struct ContentView: View {
    @State var modalDisplayed = false
    
    var body: some View {
        Button(action: { self.modalDisplayed = true }) {
            Text("Show Modal")
        }.sheet(isPresented: $modalDisplayed) {
            Form{
                Section(header: Text("Section")){
                    
                    Text("This is a test.")
                }
            }
        }
    }
}

它应该看起来如何: 在此处输入图像描述

如果模态表以横向打开然后旋转为纵向,它的外观:

在此处输入图像描述

标签: swiftxcodeformsswiftui

解决方案


.edgesIgnoringSafeArea(.all)为我解决了这个问题。(使用 Xcode 11.5 / iOS 13.5)

在此处输入图像描述在此处输入图像描述

struct ContentView: View {
    @State var modalDisplayed = false
    
    var body: some View {
        Button(action: { self.modalDisplayed = true }) {
            Text("Show Modal")
        }.sheet(isPresented: self.$modalDisplayed) {
            Form {
                Section(header: Text("Section")){
                    Text("This is a test.")
                }
            }
            .edgesIgnoringSafeArea(.all)
        }
    }
}

推荐阅读