对齐和约束在Swiftui中不起作用
我只是尝试使用在-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()
}
}
}
不确定,如何对任何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()
}
}
}
Not sure, how to align this for any iPhone or iPad devices
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需在导航视图末尾添加.navigationViewStyle(stackNavigationViewStyle())。
Just add .navigationViewStyle(StackNavigationViewStyle()) at the end of your NavigationView.
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()".