Question

I'm developing an iPhone app that (only) shows a datepicker when the user is to select a date. When the datepicker is shown, the rest of the screen will dim, so there's more visual focus on the date picker. This effect is currently achieved by adding a partially opaque black button at the size of the screen, and adding the datePicker as a subview. No problem so far.

The problem is that the statusbar isn't affected. Because my statusbar is white, it only becomes more notable as the rest of the screen darkens. I want the statusbar to be dimmed / darkened as well.

I have seen an app that exactly does what I want. I searched a lot, but I don't know how to achieve it. Is there any way to set the opacity of the statusbar or to overlap it with another view?

Any help is appreciated.

Was it helpful?

Solution

It's quite simple. What you need to do is create a UIWindow with UIWindowLevelStatusBar level. This will overlap the status bar. Here is sample code:

UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

window.windowLevel = UIWindowLevelStatusBar;
window.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.7f];

[window makeKeyAndVisible];

You can add views to this window. They can even overlap the status bar. Remember to store a reference to the new window otherwise it will be deallocated.

If you want to control the status bar appearance, or get information about rotation events you should create a UIViewController and set it as a rootViewController of your winndow. Then if you want to change the status bar style to light, add this code in the view controller:

- (UIStatusBarStyle)preferredStatusBarStyle 
{
    return UIStatusBarStyleLightContent;
}

OTHER TIPS

You have several options here.

  1. Hide the status bar. This is the simplest - hide it, display your picker, select date, show the status bar, hide the picker. What more, if you put show and hide in the same animation block as the date picker show/hide, it will animate in sync.

  2. Use a window for your picker with a level of UIWindowLevelStatusBar + 1 as @reecon suggests. This complicate matters little, but not much. From experience, I recommend using a simple view controller with either your content view directly, or a root clear color view to hold the content view. You can animate the window by [UIView transitionWithView:window duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations: ^ { [window makeKeyAndVisible]; } completion:nil;. Remember to retain the window, or you will not even see it, as it is deallocated; windows are not retained by the system. Take a look at my LNWindowManager library for an even easier window presentation API.

  3. Show an overlay window on top of the statusbar frame only. This is a little easier to maintain, as you can just resize the window to the status bar frame whenever the status bar frame changes.

You can try looking at RMDateSelectionViewController which gives you the ability to pick data in UIActionSheet style (so the backgroud is dimmed).

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