NavigationView的模态演示

时间:2019-10-13 23:36:33

标签: ios swift swiftui

我需要提供一个具有ModalPresentation的视图,它包含一个NavigationView和ScrollView,因为内容将是具有许多字段的表单。

我的代码如下,并且在纵向模式下可以正常工作,但是当我更改为横向模式时,内容消失了,什么也没有显示。

struct ContentView: View {
    @State var showingDetail = false

    var body: some View {
        Button(action: {
            self.showingDetail.toggle()
        }) {
            Text("Show Detail")
        }.sheet(isPresented: $showingDetail) {
            Detail()
        }
    }
}


struct Detail: View {
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>

    var body: some View {
        NavigationView {
            ScrollView {
                VStack {
                Text("Details view")
                Text("Details view")
                }

            }
            .navigationBarTitle("Booking", displayMode: .inline)

            .navigationBarItems(trailing:
                Button(action: {
                    self.presentationMode.wrappedValue.dismiss()
                    print("close")
                }) { Image(systemName: "xmark")  }).accentColor(.pink)

        }
    }
}

任何可以同时使用的建议:纵向和横向。

  • 注意:如果我取消NavigationView,它将起作用。

更新 查看图片:以横向显示,但是如果从左侧滑动,则我希望它不滑动地显示。

Landscape

2 个答案:

答案 0 :(得分:0)

您的代码没有错。您只需要使用以下type Message @model @auth( rules: [ { allow: owner, operations: [read] } { allow: groups groups: ["Admin"] operations: [create, update, delete, read] } ] ) { id: ID! name: String! email: String! phoneNumber: String phoneCountryCode: String message: String! owner: String group: String } 模式即可支持横向显示:

.compact

在运行iOS 13.1.2的iPhone Xs Max的Xcode版本11.2 beta(11B41)上,它对我来说很好用。

enter image description here

答案 1 :(得分:0)

VStack必须对齐-> VStack(alignment: HorizontalAlignment.center)

您必须正确工作

struct Detail: View {
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>

    var body: some View {
        NavigationView {
            ScrollView {

                VStack(alignment: HorizontalAlignment.center) {
                     Text("Width: ")
                     Text("Height: ")

                 }

            }

            .navigationBarTitle("Booking", displayMode: .inline)

            .navigationBarItems(trailing:
                Button(action: {
                    self.presentationMode.wrappedValue.dismiss()
                    print("close")
                }) { Image(systemName: "xmark")  }).accentColor(.pink)



        } .environment(\.horizontalSizeClass, .compact)
    }
}

enter image description here