I have a movie view compressed at the right-bottom viewport with portrait mode. And the movie view will expand to full screen in landscape mode when user expand the movie view. I also want to lock the movie view to landscape mode when full screen no matter what orientation the device is.
Note: all my other views are in portrait mode.
I've refer to this post with this code,
app setting,
AppDelegate.swift
internal var shouldRotate = false
func application(_ application: UIApplication,
supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return shouldRotate ? .allButUpsideDown : .portrait
}
view controller,
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
}
log,
false
UIInterfaceOrientationMask(rawValue: 26)
true
UIInterfaceOrientationMask(rawValue: 26)
false
UIInterfaceOrientationMask(rawValue: 26)
I set shouldRotate to true before setorientation, this make view can change to landscape mode. And after set orientation I set shoudRotate to false to disable rotation. Then I test it, when I click the button, the movie view change to landscape, after it I rotate my device the movie view change to portrait, and locked to the portrait mode not the landscape mode.