我最初为iPad设计了我的应用程序,现在想为iPhone添加功能。有没有一种方法可以检查正在使用的设备,然后相应地显示视图?
IF current_device == iPhone THEN
DISPLAY iPhoneView
ELSE IF current_device == iPad THEN
DISPLAY iPadView
如果可能的话,我希望iPad视图仅在水平方向上可用,而iPhone视图仅在垂直方向上可用。
我最初为iPad设计了我的应用程序,现在想为iPhone添加功能。有没有一种方法可以检查正在使用的设备,然后相应地显示视图?
IF current_device == iPhone THEN
DISPLAY iPhoneView
ELSE IF current_device == iPad THEN
DISPLAY iPadView
horizontalSizeClass
的环境值。@Environment(\.horizontalSizeClass) var horizontalSizeClass
然后在你的 SwiftUI View
中像这样使用:
var body: some View {
if horizontalSizeClass == .compact {
return CompactView() // view laid out for smaller screens, like an iPhone
} else {
return RegularView() // view laid out for wide screens, like an iPad
}
}
或者,您可以使用像这样的变量根据设备的高度(或宽度)设置单独的阈值:
@State var isLargeDevice: Bool = {
if UIScreen.main.bounds.height > 800 {
return true
} else {
return false
}
}()