0
votes

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,

enter image description here

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.

1
How about the parent views of the movie view. Are you preventing them from rotating too?Scott Thompson
Yes, I think prevent them is easy than just the movie view.LF00
Is your movie view the view of a view controller and does the view controller return the proper values for shouldAutorotate and supportedInterfaceOrientations in each mode?Scott Thompson
There is a parent view of the movie view, and the parent view if a view of view controller. I've check the these and edited in the question. Check it.LF00

1 Answers

0
votes

It's caused by this function,

func application(_ application: UIApplication,
                 supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return shouldRotate ? .allButUpsideDown : .portrait
}

change .allButUpsideDown to .landscape will do.

workable code,

AppDelegate.swift

func application(_ application: UIApplication,
                 supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return shouldRotate ? .landscape : .portrait
}

view controller,

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
}