Objective-c – Add UIView Above All Other Views, Including StatusBar

cocoa-touchiphoneobjective c

I'm wanting to create a view (UIControl) which blocks all input and shows a UIActivityIndicatorView while authenticating a user.

The UIActionSheet and UIAlertView both manage to add a black semi-transparent view over the top of all other views to block input and I'd like to do similar.

I've tried adding my view to the top UIWindow in the [[UIApplication sharedApplication] windows] array, and while this does place it above the UIKeyboard if it's visible, it doesn't place it over the StatusBar (which makes sense).

My next attempt was to extend UIAlertView, remove all of its subviews and set its layer.contents = nil, then add the ActivityIndicator as a subview. This works well, but I can't seem to kill the default bouncy transition of the UIAlertView when you call it to "show".

Does anyone have any pointers towards the best way tackle this problem that gives me full control?

Oh and I know blocking input is not great but I do ensure it's for a short period of time and it has the benefit of making it clear to the user that their action, which must complete for them to progress, is processing.

Best Answer

I've done some more digging around the API's and believe I've worked it out.

In order to display a view over the entire screen you need to create your own UIWindow and set its windowLevel property to be UIWindowLevelStatusBar. You can then add your custom subviews to this window.

Note that Apple does not encourage, but neither do they prohibit the creation of multiple windows.

UIWindow *statusWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
statusWindow.windowLevel = UIWindowLevelStatusBar;
statusWindow.hidden = NO;
statusWindow.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.7];
[statusWindow makeKeyAndVisible];

When you want to remove the window from the screen it looks as though you release it from memory.

[statusWindow release];

This doesn't feel totally safe, but I don't get any errors and it seems to drop it out of the UIApplication's windows array. Please let me know if this is wrong.

Update:

One other issue I came across was the UIStatusBar not taking touch events to scroll the active UIScrollView to the top after I had displayed and removed this overlay window. The solution was to set the primary window back to the key window once the overlay had been released.

[primaryWindow makeKeyWindow];
Related Topic