[英]how to change a view from portrait mode to landscape mode and lock it?
我有一個以縱向模式在右下角視口壓縮的電影視圖。 當用戶展開影片視圖時,影片視圖將在橫向模式下擴展到全屏。 我也想在全屏模式下將電影視圖鎖定為橫向模式,無論設備的方向如何。
注意:我所有其他視圖均為縱向模式。
我已經用這段代碼引用了這篇文章 ,
應用設置
AppDelegate.swift
internal var shouldRotate = false
func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return shouldRotate ? .allButUpsideDown : .portrait
}
視圖控制器,
func expandPlayerWindow(button: UIButton) {
self.player.view.frame = CGRect(x:0, y:-20, width: UIScreen.main.bounds.maxY, height: UIScreen.main.bounds.maxX)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
print(appDelegate.shouldRotate)
print(self.supportedInterfaceOrientations)
appDelegate.shouldRotate = true // or false to disable rotation
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
print(appDelegate.shouldRotate)
print(self.supportedInterfaceOrientations)
appDelegate.shouldRotate = false
print(appDelegate.shouldRotate)
print(self.supportedInterfaceOrientations)
UIApplication.shared.isStatusBarHidden = false
}
日志,
false
UIInterfaceOrientationMask(rawValue: 26)
true
UIInterfaceOrientationMask(rawValue: 26)
false
UIInterfaceOrientationMask(rawValue: 26)
我在setorientation之前將shouldRotate設置為true,這可以使視圖更改為橫向模式。 設置方向后,我將shoudRotate設置為false以禁用旋轉。 然后我對其進行測試,當我單擊按鈕時,電影視圖將變為橫向,在旋轉設備后,電影視圖將變為縱向,並鎖定為縱向模式而不是橫向模式。
是因為這個功能
func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return shouldRotate ? .allButUpsideDown : .portrait
}
改變.allButUpsideDown
到.landscape
會做。
可行的代碼,
AppDelegate.swift
func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return shouldRotate ? .landscape : .portrait
}
視圖控制器,
func expandPlayerWindow() {
self.player.view.frame = CGRect(x:0, y:-20, width: UIScreen.main.bounds.maxY, height: UIScreen.main.bounds.maxX)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = true // or false to disable rotation
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
appDelegate.shouldRotate = true
UIApplication.shared.isStatusBarHidden = true
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.