对齐和约束在Swiftui中不起作用

发布于 2025-01-27 11:55:34 字数 1176 浏览 3 评论 0原文

我只是尝试使用在-IPHONE和iPad设备上都可以使用的段控件。 使用SwiftUi,以下是代码和屏幕截图,此代码对iPad和不同版本的iPhone设备不起作用。

struct ContentView: View {
    @State private var favoriteColor = 0
    
    var body: some View {
        NavigationView {
            HStack{
                Picker("What is your favorite color?", selection: $favoriteColor) {
                                Text("Red").tag(0)
                                Text("Green").tag(1)
                                Text("Blue").tag(2)
                            }
                            .pickerStyle(.segmented)
                            .padding(.top,10)
                 }
            }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            ContentView()
        }
    }
}

附加的屏幕截图

iPad

不确定,如何对任何iPhone或iPad设备对齐

I'm just trying to use the segment control which work on both -iPhone and iPad devices.
Using SwiftUI, below is the code and screenshot, this code does not work for iPad and different versions of iPhone devices.

struct ContentView: View {
    @State private var favoriteColor = 0
    
    var body: some View {
        NavigationView {
            HStack{
                Picker("What is your favorite color?", selection: $favoriteColor) {
                                Text("Red").tag(0)
                                Text("Green").tag(1)
                                Text("Blue").tag(2)
                            }
                            .pickerStyle(.segmented)
                            .padding(.top,10)
                 }
            }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            ContentView()
        }
    }
}

Attached screenshotenter image description here

iPad
enter image description here

Not sure, how to align this for any iPhone or iPad devices

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我做我的改变 2025-02-03 11:55:34

您的iPad视图看起来不合适,因为您没有将导航视图送给您的导航视图。

            NavigationView {
               HStack{
                   Picker("What is your favorite color?", selection: $favoriteColor) {
                                   Text("Red").tag(0)
                                   Text("Green").tag(1)
                                   Text("Blue").tag(2)
                               }
                               .pickerStyle(.segmented)
                               .padding(.top,10)
                    }
    }.navigationViewStyle(StackNavigationViewStyle())

只需在导航视图末尾添加.navigationViewStyle(stackNavigationViewStyle())。

enter image description hereYour ipad view is not looking proper as you want Because you did not gave navigationViewStyle to your navigation View.

            NavigationView {
               HStack{
                   Picker("What is your favorite color?", selection: $favoriteColor) {
                                   Text("Red").tag(0)
                                   Text("Green").tag(1)
                                   Text("Blue").tag(2)
                               }
                               .pickerStyle(.segmented)
                               .padding(.top,10)
                    }
    }.navigationViewStyle(StackNavigationViewStyle())

Just add .navigationViewStyle(StackNavigationViewStyle()) at the end of your NavigationView.

半透明的墙 2025-02-03 11:55:34

AS namra parmar 您需要设置(覆盖)iPad的默认导航样式,因为默认情况下,默认情况下,视图在iPhone上堆叠起来,但它们成为iPad上的列。
以下是一些具有不同屏幕尺寸的设备:iPhone SE 1st Gen(4.0英寸),iPhone 13 Pro(6.06英寸),iPhone 13 Pro Max(6.68inch),iPad Pro(12.9inch),当您将NavigationViewStyle设置为“ StackNavigation Viewledle” ()。

As Namra Parmar said you need to set (override) the default navigation style of the iPad, because, by default, views are stacked up on iPhone, but they become columns on the iPad.
Here are some devices with different screen sizes: iPhone SE 1st gen (4.0inch), iPhone 13 Pro (6.06inch), iPhone 13 Pro Max (6.68inch), iPad pro (12.9inch), when you set the navigationViewStyle to "StackNavigationViewStyle()".

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文