Llg Llg - 9 months ago 86
iOS Question

Status bar is landscape after rotation in only portrait view

I have allowed all viewControllers in my swift application to be in portrait mode. I did this programatically, because some viewControllers contain videos that I want to be played in both, landscape and portrait mode. My problem is that when I rotate the device, the view stays in the portrait mode (that's what I want) but the status bar of the device is shown in landscape mode.

This is the code in Swift 3 that I have added to make the view in the only portrait mode:

override var shouldAutorotate: Bool {
return false

override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait

Any help please?

On my AppDelegate I specified my initial view:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "firstViewController")
self.window?.rootViewController = initialViewController
return true

Then in my firstViewController I can go to another view via a button click like this:

let controller = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController
self.present(controller, animated: false, completion: nil)

Or I show some details in a child view (that is opened after a button click):

let vc = self.storyboard?.instantiateViewController(withIdentifier: "detailViewController") as! DetailViewController
vc.didMove(toParentViewController: self)

Answer Source

shouldAutorotate should be in the rootViewController because the top viewController will fore it to all of its sub controllers