Add UIView banner above status bar iOS 7
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
}
Related videos on Youtube
Comments
-
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
toNO
. This is not only really annoying to have to change all my different view controllersprefersStatusBarHidden
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:
-
mverderese about 10 yearsThanks 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 about 10 yearsNevermind. I was doing
[window makeKeyAndVisible]
instead of justwindow.hidden = NO;
Works perfectly! -
Weizhi almost 10 yearsHey @dezinezync , how to add my custom
UIWindow
to my view when I want to show that custom status view? -
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 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 over 9 yearsuiwindow over status works fine but only in portrait mode. how to handle the rotation? please check my question stackoverflow.com/questions/26976494/…
-
boweidmann over 9 yearsThanks 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 almost 6 yearsWhat to do with iPhone X
-
Thomas Paul almost 6 yearsYou need to add 30px top padding to the banner view content while using this on iphone x.