How to set unsafe area background color for ios 11

19,274

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:

enter image description here

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)

Share:
19,274
thibaut noah
Author by

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, 2022

Comments

  • thibaut noah
    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
    thibaut noah over 6 years
    Damn, got it you have to press ctrl to get the container margin, damn system set top to safe area by default, thanks
  • pesch
    pesch over 6 years
    If 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
    thibaut noah over 6 years
    I will check that.
  • thibaut noah
    thibaut noah over 6 years
    Awesome ! Thanks mate
  • sgelves
    sgelves almost 5 years
    So, it is the view background color property the key to color the Status bar.
  • Paweł Kanarek
    Paweł Kanarek over 4 years
    Dosen'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
    Krunal over 4 years
    @PawełKanarek - Thank you for raising a query. I'll check and update this answer.
  • thibaut noah
    thibaut noah over 4 years
    You'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
    Pravin Tate over 4 years
    @PawełKanarek Did you get anything around this ?
  • Paweł Kanarek
    Paweł Kanarek over 4 years
    No, 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
    TM Lynch over 4 years