我将一些 View 设为横向,另一些设为纵向。 我的问题是我不想让它们轮换。 如果页面是横向的,则仅保持横向,对于纵向页面也如此。 我搜索了很多并尝试过:
override func shouldAutorotate() -> Bool {
return false
}
和
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait
}
但是没有成功
最佳答案
只需通过覆盖获取方向:
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")
}
}
并做这样的事情。
此功能不会锁定您的屏幕,但会将屏幕保持在您想要的方向。
关于swift - 快速锁定某些 View 的旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45545918/