How to set unsafe area background color for ios 11
Solution 1
It looks like a hacky trick but you may try this:
You can set background color for status bar during application launch or during viewDidLoad of your view controller. Here it works for me, in following ways.
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.statusBarView?.backgroundColor = UIColor.green
}
}
or
// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.green
return true
}
}
Here is result:
Solution 2
You have to apply different constraints. Your background color should extend beyond the safe area all the way to the superview. So your constraints need to be set to the superview for your background color but to the safe area for your ui view (buttons, tableViews and the like)
![thibaut noah](https://lh4.googleusercontent.com/-ueWzXuRLcv0/AAAAAAAAAAI/AAAAAAAAABE/sizg7GsU1_U/photo.jpg?sz=256)
thibaut noah
BY DAY : developper student at the french it school 42, intern at the startup smartertime (working on the portage of their self quantified app from android to ios 9 with swift 2.0) BY NIGHT : whitehat, sysadmin, docker enthousiast For Fun : hardcore gamer (former top world player), japanese culture, hardcore, extrem sports, ring training
Updated on July 07, 2022Comments
-
thibaut noah almost 2 years
Creating some new view controllers with xcode 9 so now I have a few safe areas to deal with.
I am currently trying to do something fullproof, meaning keeping the unsafe area as it is (since I always display the status bar) and having the background color extending to the fullscreen (to keep a similar behaviour to what I used to have).
On an additional note, this also affect page controls since when you have some the system will put them in the bottom unsafe area which will also be displayed in black.
I cannot find a way for the background color to extend behind the unsafe area though. Any thoughts?
-
thibaut noah over 6 yearsDamn, got it you have to press ctrl to get the container margin, damn system set top to safe area by default, thanks
-
pesch over 6 yearsIf you are doing this in IB, you can also select your constraint in the Document Outline and use the Attributes Inspector to change from Superview to Safe Area and so forth
-
thibaut noah over 6 yearsI will check that.
-
thibaut noah over 6 yearsAwesome ! Thanks mate
-
sgelves almost 5 yearsSo, it is the view background color property the key to color the Status bar.
-
Paweł Kanarek over 4 yearsDosen't work on iOS13 Exception: NSInternalInconsistencyException Reason: App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.
-
Krunal over 4 years@PawełKanarek - Thank you for raising a query. I'll check and update this answer.
-
thibaut noah over 4 yearsYou're a little late to the party mate. The question is not related to ios 13, neither is it related to the navigationBar.
-
Pravin Tate over 4 years@PawełKanarek Did you get anything around this ?
-
Paweł Kanarek over 4 yearsNo, i solved problem by digging into Xamain.Forms code to hack it out. Don't know about solution to native platforms. I was just posting about Exception.
-
TM Lynch over 4 yearsThe answer is now here: stackoverflow.com/questions/57943692/…