gpt4 book ai didi

swift - 快速锁定旋转一些 View

转载 作者:可可西里 更新时间:2023-11-01 00:56:45 26 4
gpt4 key购买 nike

我将一些观点设为风景,另一些设为肖像。我的问题是我不想让它们旋转。如果页面是横向的,则只保留横向页面,纵向页面也一样。我搜索了很多,我试过了:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com