Search code examples
iosswiftuikit

How to set safe area background color in swift 4.2?


I'm a very beginner in swift

I have problem with the safe area view.

safe area view

For the top(some said "Status bar") I've done with this code for changing the background color

if #available(iOS 13.0, *) {
            let app = UIApplication.shared
            let statusBarHeight: CGFloat = app.statusBarFrame.size.height

            let statusbarView = UIView()
            statusbarView.backgroundColor = hexStringToUIColor(hex: "#7f0000")
            view.addSubview(statusbarView)

            statusbarView.translatesAutoresizingMaskIntoConstraints = false
            statusbarView.heightAnchor
                .constraint(equalToConstant: statusBarHeight).isActive = true
            statusbarView.widthAnchor
                .constraint(equalTo: view.widthAnchor, multiplier: 1.0).isActive = true
            statusbarView.topAnchor
                .constraint(equalTo: view.topAnchor).isActive = true
            statusbarView.centerXAnchor
                .constraint(equalTo: view.centerXAnchor).isActive = true

        } else {
            let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
            statusBar?.backgroundColor = hexStringToUIColor(hex: "#7f0000")
        }

I have no idea how to change the bottom safe area view background color?

my Main.storyboard my storyboard


Solution

  • just set the color to your view: view.backgroundColor = .blue

    and set constraints to your view, that it is as big as the safe area.