[英]viewWillTransitionToSize for locked orientation
即使用戶鎖定了方向,是否有任何方法可以檢測屏幕方向? 我的應用程序使用自定義的相機視圖,我希望能夠根據方向以橫向或縱向保存圖像。 我目前正在使用以下代碼來檢測方向,但是,只有在用戶未鎖定它的情況下,這才起作用。
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
let orientation: UIDeviceOrientation = UIDevice.current.orientation
switch (orientation) {
case .portrait:
print("Portrait")
case .landscapeRight:
print("Left")
case .landscapeLeft:
print("Right")
default:break
}
}
如果這不是使用相機方向的正確方法,請以正確的方向引導我。
我相信做到這一點的唯一方法是在項目中同時啟用縱向和橫向模式:
然后,在不需要橫向模式的所有視圖控制器中覆蓋這些屬性。
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}
在帶有嵌入式攝像頭的視圖控制器中,只需將shouldAutorotate設置為false並添加用於風景中的額外supportedInterfaceOrientations 。
請注意,如果您使用UINavigationController或UITabBarController,它將無法正常工作,因此您應該創建以下擴展名: https : //stackoverflow.com/a/39674618/5381663
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.