25

I'm trying to add a banner above the status bar when receiving an in-app push notification. From what I've been reading, it seems like the only way to dynamically change the status bar style in iOS 7 is to set UIViewControllerBasedStatusBarAppearance to NO. This is not only really annoying to have to change all my different view controllers prefersStatusBarHidden to [UIApplication sharedApplication].statusBarHidden, but it also doesn't give the effect I'm looking for.

When the banner slides from the top, I still want the 20 pts of space that the status bar provides to stay, but the status bar content to disappear until after the banner slides back up. Is there a way to either do this or add a subview or window above the status bar?

Basically I'm looking to do this:

Facebook Messenger push banner

mverderese
  • 5,314
  • 6
  • 27
  • 36

3 Answers3

46

To put your view controller above status bar:

[[[[UIApplication sharedApplication] delegate] window] setWindowLevel:UIWindowLevelStatusBar+1];

To put your view controller behind status bar:

[[[[UIApplication sharedApplication] delegate] window] setWindowLevel:UIWindowLevelNormal];
Nico
  • 1,580
  • 14
  • 21
  • 1
    Thanks Nico!! To make screenshots for launch images it's very helpful to make status bar transparent or hide it behind the window! You saved my day – boweidmann Nov 22 '14 at 07:38
33

Create a new window and add your banner view to that window. When you need to show the banner, you can set yourwindow.hidden = NO; You can further add animations to showing it and to dismiss it yourwindow.hidden = YES;.

The key here is is setting yourwindow.windowLevel = UIWindowLevelStatusBar+1;

That will make sure your banner view and the yourwindow always appear above the status bar.

Feel free to ask questions regarding any of the above.

dezinezync
  • 2,730
  • 21
  • 15
  • Thanks a lot! The line `yourwindow.windowLevel = UIWindowLevelStatusBar+1;` was the key. As a follow up, the status bar style is white before the banner shows, but changes to black once it shows and disappears. How can I force the status bar to turn white again without having to leave the screen and come back? – mverderese Mar 07 '14 at 08:20
  • Nevermind. I was doing `[window makeKeyAndVisible]` instead of just `window.hidden = NO;` Works perfectly! – mverderese Mar 07 '14 at 08:29
  • Hey @dezinezync , how to add my custom `UIWindow` to my view when I want to show that custom status view? – Weizhi Jul 24 '14 at 14:38
  • 1
    @dee you don't add it to any particular view. You simply use myWindow.hidden = NO. If you'd like, you can position the frame of the window in a way that it appears as a part of your view. I could be wrong, and there could be a better way given that UIWindow is a specialized UIView, but at the time of writing this, that's how I'd do it. – dezinezync Jul 24 '14 at 14:42
  • @dezinezync , thanks for your answer. I'm getting confused, there's no need to add the window to it's superview to show it? I follower your tip , and I can show the window without attaching it to a superview , but when I update the window's subview , the subview won't be changed.Why? – Weizhi Jul 25 '14 at 07:05
  • uiwindow over status works fine but only in portrait mode. how to handle the rotation? please check my question http://stackoverflow.com/questions/26976494/ios-8-7-uiwindow-with-uiwindowlevelstatusbar-rotation-issue – Hashmat Khalil Nov 18 '14 at 07:49
4

Add UIView banner above status bar-Swift4

func showBannerView(bannerView:UIView){
     let window = UIApplication.shared.keyWindow!
    window.addSubview(bannerView)
    window.windowLevel = UIWindowLevelStatusBar+1
}
func removeBannerView(bannerView:UIView){
    bannerView.removeFromSuperview()
    let window = UIApplication.shared.keyWindow!
    window.windowLevel = UIWindowLevelStatusBar - 1

} 
Thomas Paul
  • 355
  • 5
  • 13