Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set unsafe area background color for ios 11

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?

like image 376
thibaut noah Avatar asked Oct 03 '17 10:10

thibaut noah


2 Answers

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

like image 91
Krunal Avatar answered Oct 05 '22 01:10

Krunal


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)

like image 23
pesch Avatar answered Oct 04 '22 23:10

pesch