在我的应用中所有的屏幕都是纵向的。如何只允许单个UIViewController在两个方向模式:纵向和上下颠倒?当用户旋转iPhone时,UIViewController也应该旋转。
首先,在你的应用程序中,除了一个屏幕外,所有屏幕都是纵向的。你不能将应用程序的朝向设置为竖屏。设置设备方向为纵向和横向。
将以下代码放在ViewDidLoad
中需要横向定向的屏幕上let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
和
override var shouldAutorotate: Bool {
return true
}
在viewDidLoad
中把下面的代码放到所有你只需要纵向方向的屏幕上let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
如果你有一个连续调用的函数,那么你可以在这个函数中检查设备的方向-
if UIDevice.current.orientation == UIDeviceOrientation.portraitUpsideDown {
// set your view constraints
}
if UIDevice.current.orientation == UIDeviceOrientation.portrait {
// set your view constraints
}
否则,你应该创建一个监视函数,并在函数中检查设备的方向。