问题
I know there have been some questions posted about this, but none have helped me with my specific issue. I would like to display a custom modal above the entire screen, but I would like to keep the Apple status bar visible. For the modal, I am using one UIView
for the dimming effect and another for the actual view that the user would interact with. The entire modal is then added as a subview to the current view and brought to the front. Essentially, I am trying to replicate the behavior of the UIAlertView, except with a custom view. Here is some of my code:
var modalView:UIView = UIView(frame: self.view.window!.bounds)
modalView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.66)
modalView.addSubview(customView)
self.view.window!.addSubview(modalView)
modalView.window!.windowLevel = UIWindowLevelStatusBar + 1
self.bringSubviewToFront(modalView)
Above, customView
is the UIView
that the user interacts with.
This works great, but for some reason, the text on the Apple status bar just simply disappears even though the style of the status bar is set to LightContent. As can be seen in the code, I do not touch the status bar.
I am trying to get the text on the status bar to dim just like the rest of the screen and am currently stuck. Does anyone have any insight on how I can get the desired behavior?
Any help would be greatly appreciated!
回答1:
Updated Dec. 2017 • Swift 4 • iOS 10.0+
I briefly tried the accepted answer's edited solution, but I couldn't easily make an entire viewController for just a UIView banner. However, by accessing the statusBar
value in UIApplication
, I was able to add a UIView as a subview to statusBarView
. This will put the UIView on top of the status bar. As far as I know, this is a relatively stable workaround that has worked for the last few iOS versions.
extension UIApplication {
/// Returns the status bar UIView
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
Usage
let customBannerView = CustomStatusView(with: message)
UIApplication.shared.statusBarView?.addSubview(customBannerView)
回答2:
EDIT (November 16, 2015)
I am copying my answer from this thread below:
This answer breaks in iOS 8.3+ and possibly previous versions of iOS 8. I DO NOT recommend that anyone uses it, especially since it is not guaranteed to work in future iOS versions.
My new solution uses the standard presentViewController
method of a UIViewController
. I initialize a UIViewController
, add my custom modal View
as a subview of the UIViewController
, and then optionally position the modal View
using constraints. Works like a charm!
Original Answer
Following Anna's advice, I got it working by using a UIWindow
instead of a UIView.
Here's my new code:
var modalWindow:UIWindow = UIWindow(frame: self.view.window!.bounds)
modalWindow.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.66)
modalWindow.hidden = false
modalWindow.windowLevel = (UIWindowLevelStatusBar + 1)
modalWindow.addSubview(customView)
modalWindow.makeKeyAndVisible()
It now works exactly as before, except the text on the Apple status bar gets dimmed as well. Thank you very much for all of the help!
来源:https://stackoverflow.com/questions/26781837/display-uiview-above-apple-status-bar-in-ios-8