Search code examples
iosipaduiviewcontrollerrotationuiwindow

Rotating a UIView that is added to keyWindow


I am designing an iOS Passcode Lock library with a "cover view" feature, which hides the content on the screen, when the app is backgrounded. See this image.

When my PasscodeManager receives the UIApplicationWillResignActiveNotification notification, it does this:

[UIApplication.sharedApplication.keyWindow addSubview:self.coverView];

This works great, does exactly what I want to do! However, if the iPad is in landscape orientation, this view does not rotate, and looks horrible on the iOS 7 multitasking preview interface.

I know that UIViewController handles rotations, but I am not sure how to use it for this purpose, because I can't seem to push a UIViewController when the app is getting backgrounded (it looks like 2 view controllers are on top of each other, each half visible).


Solution

  • UIViewController takes care of rotating its view. If you are adding subviews directly to the window, you don't get that behavior. So, you have two choices: either use a UIViewController, or handle the rotation yourself. If you want to handle it yourself you can listen for UIDeviceOrientationDidChangeNotification. You may also need to call beginGeneratingDeviceOrientationNotifications. See https://developer.apple.com/library/ios/documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html