iosswiftxcodesafearealayoutguide

How can I avoid covering the safe area on iPhones, but at least change their color?


I've noticed that some iPhone apps cannot not only stay away from the safe areas on the iPhone, but they also change the background color of the safe areas, whilst not covering the upper status bar info, icons, time, etc.

I can make my background totally cover the entire screen, but then the iOS' top info disappears.

I cannot find how to do this using only Xcode. I'm building everything via code, no use of storyboards.

As a bonus would also love to know if I can change the view, or game scene, coordinates so that 0,0 (lower left) is above the safe area?

This is the code I found via Google, it stays out ofd the top area but not the bottom. Yes, I just threw it in their I have to stay this more.

What I did notice was that even though I stayed out of the top safe area, the iOS's info, icons, etc. did not appear.

view.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
imageView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50).isActive = true
 imageView.widthAnchor.constraint(equalToConstant: 200).isActive = true
 imageView.heightAnchor.constraint(equalToConstant: 200).isActive = true
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
imageView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 50).isActive = true

Solution

  • Here is what you need to try

        mview.translatesAutoresizingMaskIntoConstraints = false
        mview.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor).isActive = true
        mview.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor).isActive = true
        mview.bottomAnchor.constraint(equalToSystemSpacingBelow:view.safeAreaLayoutGuide.bottomAnchor, multiplier: 1.0).isActive = true
        mview.heightAnchor.constraint(equalToConstant: 300).isActive = true
    

    Note: I dont know why your both instances names are same ... "view"