锁定旋转一些视图 swift
lock rotation some views swift
我将我的一些观点设为横向,另一些设为纵向。
我的问题是我不想让它们旋转。
如果页面是横向的,则只保留横向页面,纵向页面也一样。
我搜索了很多,我试过了:
override func shouldAutorotate() -> Bool {
return false
}
和
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait
}
但是没用
您的方向是正确的,但是当用户处于纵向方式时,并且由于您的视图是横向的而您想强制用户横向,您必须更改 UIDevice
方向。
UIDevice.current.setValue(UIDeviceOrientation.landscapeLeft.rawValue, forKey: "orientation")
有关更多 UIDevice 选项,请参阅苹果文档:
https://developer.apple.com/documentation/uikit/uideviceorientation
为确保用户无法返回纵向,您添加:(您已有的代码)
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.landscape
}
添加此 属性 并在 AppDelegate
中运行
internal var shouldRotate = false
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return shouldRotate ? .landscape : .portrait
}
无论你想在哪里调用这条线
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = true
通过覆盖获取方向:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
if UIDevice.current.orientation.isLandscape && flag{
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
print("Landscape")
} else if UIDevice.current.orientation.isPortrait && !flag {
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
print("Portrait")
}
}
然后做这样的事情。
此功能不会锁定您的屏幕,但它会使屏幕保持在您想要的方向。
我将我的一些观点设为横向,另一些设为纵向。 我的问题是我不想让它们旋转。 如果页面是横向的,则只保留横向页面,纵向页面也一样。 我搜索了很多,我试过了:
override func shouldAutorotate() -> Bool {
return false
}
和
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait
}
但是没用
您的方向是正确的,但是当用户处于纵向方式时,并且由于您的视图是横向的而您想强制用户横向,您必须更改 UIDevice
方向。
UIDevice.current.setValue(UIDeviceOrientation.landscapeLeft.rawValue, forKey: "orientation")
有关更多 UIDevice 选项,请参阅苹果文档: https://developer.apple.com/documentation/uikit/uideviceorientation
为确保用户无法返回纵向,您添加:(您已有的代码)
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.landscape
}
添加此 属性 并在 AppDelegate
中运行 internal var shouldRotate = false
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return shouldRotate ? .landscape : .portrait
}
无论你想在哪里调用这条线
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = true
通过覆盖获取方向:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
if UIDevice.current.orientation.isLandscape && flag{
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
print("Landscape")
} else if UIDevice.current.orientation.isPortrait && !flag {
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
print("Portrait")
}
}
然后做这样的事情。
此功能不会锁定您的屏幕,但它会使屏幕保持在您想要的方向。