Add UIView banner above status bar iOS 7

16,149

Solution 1

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.

Solution 2

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];

Solution 3

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

} 
Share:
16,149

Related videos on Youtube

mverderese
Author by

mverderese

@mverderese on Twitter

Updated on September 16, 2022

Comments

  • mverderese
    mverderese over 1 year

    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
    mverderese about 10 years
    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
    mverderese about 10 years
    Nevermind. I was doing [window makeKeyAndVisible] instead of just window.hidden = NO; Works perfectly!
  • Weizhi
    Weizhi almost 10 years
    Hey @dezinezync , how to add my custom UIWindow to my view when I want to show that custom status view?
  • dezinezync
    dezinezync almost 10 years
    @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.
  • Weizhi
    Weizhi almost 10 years
    @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?
  • Hashmat Khalil
    Hashmat Khalil over 9 years
    uiwindow over status works fine but only in portrait mode. how to handle the rotation? please check my question stackoverflow.com/questions/26976494/…
  • boweidmann
    boweidmann over 9 years
    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
  • christijk
    christijk almost 6 years
    What to do with iPhone X
  • Thomas Paul
    Thomas Paul almost 6 years
    You need to add 30px top padding to the banner view content while using this on iphone x.