![](/img/trans.png)
[英]App support the app Landscape and portrait mode in both ipad and iphone
[英]SwiftUI: Make View portrait only on the iPhone, but both portrait and landscape on the iPad
我有一个特定的 SwiftUI 视图,在 iPad 上需要横向和纵向,但在 iPhone 上应该只是纵向。 我已经看到了一些在 UIKit 中实现它的方法,但与 SwiftUI 没有任何关系。 有可能这样做吗?
您可以使用UIDevice.current.model
来检查设备类型。 因此,当您拥有两个视图(可旋转和不可旋转)时,您可以根据设备类型将它们放入if...else
语句中:
struct RotateOniPadOnly: View {
private var needsToRotate: Bool {
return UIDevice.current.model == "iPad"
}
var body: some View {
if needsToRotate {
return AnyView(Text("rotated text")
.font(.system(size: 100))
.rotationEffect(Angle(degrees: 180)))
} else {
return AnyView(Text("not rotated text")
.font(.system(size: 50)))
}
}
}
struct RotateOniPadOnly_Previews: PreviewProvider {
static var previews: some View {
Group {
RotateOniPadOnly()
.previewDevice("iPad Pro (12.9-inch) (3rd generation)")
RotateOniPadOnly()
}
}
}
结果应该是:
根据问题的编辑更新我可以建议更改视图布局以响应尺寸类教程,它基于@Environment(\\.horizontalSizeClass) var sizeClass
,我认为您可以从中找到解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.