Question

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).

Was it helpful?

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

OTHER TIPS

Do not add subviews to the key window. This is messy, you will not hear orientation changes and you will not hide alert views and other elements displayed in different windows.

Instead, display another window with a higher window level (such as UIWindowLevelAlertView). This view will have its own view controller, which will hear all rotation notifications normally.

Take a look at my LNWindowManager helper class. You can use it as a much simplified API for presenting and dismissing windows on top of other windows. When you app enters the background, display the lock window with no animation. When the user unlocks, dismiss it with animation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top