简体   繁体   English

SwiftUI:如何仅为 iPad 自定义工作表?

[英]SwiftUI: How can I customize the sheet only for iPad?

I´m new in SwiftUI and I have the following problem.我是 SwiftUI 的新手,但遇到以下问题。 When I click the button on the iPhone the following comes up当我单击 iPhone 上的按钮时,会出现以下内容

在此处输入图片说明

but when I press the button on the iPad the following comes up但是当我按下 iPad 上的按钮时,会出现以下情况

在此处输入图片说明

The picker is not on the whole sheet.选择器不在整个工作表上。 How can I show the picker on the iPad on the whole sheet like on the iPhone?如何像在 iPhone 上一样在整个工作表上显示 iPad 上的选择器?

    struct ContentView: View {
    @State var value_2 = 1
    @State var show_1 = false
    
    var body: some View {
        VStack {
            Button(action: {
                self.show_1.toggle()
            }) {
                    Text("Push")
                }
            .sheet(isPresented: $show_1) {
                Sheet(show_0: self.$show_1, value_1: self.$value_2)
            }
        }
    }
}

struct Sheet: View {
    @Binding var show_0: Bool
    @Binding var value_1: Int
    
    var body: some View {
        NavigationView {
            number(value_0: $value_1)
                .navigationBarTitle(Text("Enter number"), displayMode: .inline)
                .navigationBarItems(trailing: Button(action: {
                    self.show_0 = false
                }) {
                    Text("Done").bold()
            })
        }
    }
}

struct number: View {
    @Binding var value_0: Int
    
    var body: some View {
        Section {
            Text("Headline")
            Picker("",selection: $value_0)
                    {
                        ForEach(1..<101) { value in
                            Text("\(value)")
                }
            }
        }
        .labelsHidden()
    }
}

As far as I understood your intention, you need just change a style of navigation view, like据我了解您的意图,您只需要更改导航视图的样式,例如

var body: some View {
    NavigationView {
        number(value_0: $value_1)
            .navigationBarTitle(Text("Enter number"), displayMode: .inline)
            .navigationBarItems(trailing: Button(action: {
                self.show_0 = false
            }) {
                Text("Done").bold()
        })
    }
    .navigationViewStyle(StackNavigationViewStyle())    // << here !!
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM