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.
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;
}