3

I have an application where I have a use case for two UIWindow instances. The first instance runs all normal app interactions, but I need to be able to present a view controller above everything else regardless of what the user is doing in the application, without user interaction.

I have the second window working and presenting fine. In this view controller, I only support portrait mode (though other parts of the app support landscape). I've tried to block rotation using these methods in the rootViewController of the window:

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
  return [.Portrait]
}

override func shouldAutorotate() -> Bool {
  return false
}

That successfully prevents the device from rotating the view controller, but it still allows the status bar to rotate. I'd like to prevent the status bar from rotating as well. I tried adding the following to my AppDelegate

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
  if let _ = window as? MyTopWindow {
    return [.Portrait]
  } else {
    return .All
  }
}

However that hasn't worked either. The status bar still rotates.

I've seen the question below and tried to implement the solution but it's not working for my case: Status bar rotates separated from UIWindow

Edit

Here's the code where I create the UIWindow:

self.callManagementWindow = ActiveCallManagementWindow(frame: UIScreen.mainScreen().bounds)
self.callManagementWindow.rootViewController = primaryViewController
self.callManagementWindow.hidden = false
self.callManagementWindow.makeKeyAndVisible()
Community
  • 1
  • 1
Colin M
  • 13,010
  • 3
  • 38
  • 58
  • I should add - if anyone knows of a way I can implement this _without_ using multiple `UIWindow` instances, I'm up for that as well. However, I don't know of any other way to be able to ensure that I can always present a view controller on top of everything else. – Colin M May 06 '16 at 23:25
  • Can you post the code where you're creating and setting up the UIWindows? – kpsharp May 10 '16 at 18:07
  • @kpsharp Added the code for that – Colin M May 11 '16 at 12:46
  • Try this: [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait; If not resolved , then can you post the code for the remaining UIWindow ? – itechnician May 11 '16 at 13:00

3 Answers3

4

Some time ago I also had issues with two windows and rotation and if I recall right you need to override supportedInterfaceOrientations() and shouldAutorotate() in rootViewController of first UIWindow as well as second one

sage444
  • 5,661
  • 4
  • 33
  • 60
  • I was hoping to not have to do this, but it looks like this is required. I've adjusted my code so when the main `UIWindow` is in the background, those `UIViewControllers` return correct values in `supportedInterfaceOrientations` and `shouldAutorotate` that suit the key window. – Colin M May 13 '16 at 13:15
1

Can you give a try to this in primaryViewController's viewDidAppear :

[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
itechnician
  • 1,645
  • 1
  • 14
  • 24
-1

In my case, I setup UIWindow in code, while also specifying Main storyboard in Info.plist

<key>UIMainStoryboardFile</key>
    <string>Main</string>

The Main.storyboard has a simple initial ViewController (this ViewController does not specify any orientations)

The solution is removing the UIMainStoryboardFile (in Info.plist or in General -> Targets -> Deployment Info -> Main Interface)

Vignesh Sundaramoorthy
  • 1,827
  • 1
  • 25
  • 38