3

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

saleumsack
  • 43
  • 1
  • 1
  • 8

6 Answers6

8

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.

Chris
  • 7,579
  • 3
  • 18
  • 38
3

I was struggling with this one for an hour so I thought I'd share my solution if anyone wants it.

This DID NOT work for me

extension UIApplication
{
    var statusBarView: UIView?
    {
        return value(forKey: "statusBar") as? UIView
    }
}
// Calling this  crashed
// UIApplication.shared.statusBarView?.backgroundColor = UIColor.black

So I just made a different extension

extension UIViewController
{
  func topColouredBlack()
  {
     let colouredTopBlack = UIView()
     view.addSubview(colouredTopBlack)
     colouredTopBlack.translatesAutoresizingMaskIntoConstraints = false
     colouredTopBlack.backgroundColor = .black

     NSLayoutConstraint.activate([
        colouredTopBlack.topAnchor.constraint(equalTo: view.topAnchor),
        colouredTopBlack.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
        colouredTopBlack.widthAnchor.constraint(equalTo: view.widthAnchor),
    ])
  }
}

I just call topColouredBlack() wherever I want & I made another one of a different colour when I want to change it back.

func scrollViewWillBeginDragging(_ scrollView: UIScrollView)
{
    topColouredBlack()
}

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool)
{
  // topColouredWhite()
}
daj mi spokój
  • 248
  • 1
  • 2
  • 8
2

have you tried this code?

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
  }
}
Vamsi S
  • 269
  • 3
  • 16
Ali Pishvaee
  • 303
  • 5
  • 7
0

Though my solution here is solved on post iOS 13, I think this may help:

https://stackoverflow.com/a/66148059/4963585

Basically the solution is make a view with the same background color that constrains to safe area's top, then activate that additional view's topAnchor. If your view requires scrolling, you can also set the anchor between the original background and that added view to prevent scrolling gap.

whitney13625
  • 583
  • 1
  • 9
  • 28
0

Just set color of safeAreaLayoutGuide for whole app in SceneDelegate:

  window?.safeAreaLayoutGuide.owningView?.backgroundColor = .green
-1

let app = UIApplication.shared let statusBarView = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: app.statusBarFrame.height)) statusBarView.backgroundColor = UIColor.red strong textself.view.addSubview(statusBarView)

you can use this code it work for me

Kevin Pham
  • 19
  • 5