How to set unsafe area background color for ios 11
Asked Answered
I

5

17

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?

Inadvertency answered 3/10, 2017 at 10:8 Comment(0)
O
21

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

Ossicle answered 3/10, 2017 at 10:44 Comment(6)
Awesome ! Thanks mateInadvertency
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.Portend
@PawełKanarek - Thank you for raising a query. I'll check and update this answer.Ossicle
@PawełKanarek Did you get anything around this ?Fortuity
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.Portend
The answer is now here: #57944192Annotate
S
10

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)

Steeplebush answered 3/10, 2017 at 10:25 Comment(4)
Damn, got it you have to press ctrl to get the container margin, damn system set top to safe area by default, thanksInadvertency
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 forthSteeplebush
I will check that.Inadvertency
So, it is the view background color property the key to color the Status bar.Cons
S
0

Recently I was solving a problem similar to this. The difference is that in my view the top unsafe areas has to be filled while the bottom ones doesn't. Another difference is that my background view is actually in a scrollView, which make the constraints setting more complex.

I tried the "statusBar" solution mentioned above, however I guess the view hierarchy had be changed since iOS 13, thus this would result to crash.

Finally I came out with this solution that solves the problem:

Firstly add Status Bar Bg View at the very top of the view controller view, align its top, leading, and trailing to safe area's top, leading, and trailing (statusBarBgView in the code). At the same time, set the original fill background (bgView)'s top constrains to Status Bar Bg View's bottom.

Then in viewDidLoad(), call the following method:

private func fillSafeAreaIfNeeded() {
    if let _ = UIApplication.shared.keyWindow?.safeAreaInsets.top {
        statusBarBgView.translatesAutoresizingMaskIntoConstraints = false
        statusBarBgView.topAnchor.constraint(equalTo: view!.topAnchor).isActive = true // This line fills up status bar
        bgView.topAnchor.constraint(equalTo: statusBarBgView!.bottomAnchor).isActive = true
    }
    else {
        statusBarBgView.frame = CGRect.zero
    }
}

Note that by constraining bgView's topAnchor with statusBarBgView's bottomAnchor, the gap between both view could be avoided while scrolling.

Socorrosocotra answered 11/2, 2021 at 2:43 Comment(0)
P
0

For those who are still looking up this question. My simplest solution is to create an uiview and constraint it

    private let containerView: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = //Your background color you want present
    return view
}()

Then constraints it in this way. Set bottomSafeArea(or topSafeArea)

        containerView.topAnchor.constraint(equalTo: view.topAnchor),
        containerView.leftAnchor.constraint(equalTo: view.leftAnchor),
        containerView.rightAnchor.constraint(equalTo: view.rightAnchor),
        containerView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),

and finally set color to it

containerView.backgroundColor = //Color you want to set
Philosophize answered 19/10, 2023 at 5:56 Comment(0)
H
-1
    if #available(iOS 13.0, *) {
        let navBarAppearance = UINavigationBarAppearance()
        navBarAppearance.configureWithTransparentBackground()
        navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
        navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
        navBarAppearance.backgroundColor = .black
        navigationController?.navigationBar.standardAppearance = navBarAppearance
        navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
    }
Hendrick answered 24/10, 2019 at 7:33 Comment(1)
You're a little late to the party mate. The question is not related to ios 13, neither is it related to the navigationBar.Inadvertency

© 2022 - 2025 — McMap. All rights reserved.