swiftsafearealayoutguide

Don't understand why my UILabel won't follow safeareaLayout constraints


Am not working with storyboards, and below is the full code for my UIViewController for my Main Menu screen. While everything appears to work, I made an error, but don't understand the outcome.

myView, the gray area is set to the safeareaLayout constraints
fillRects is a function where I prefill all the rects for the labels and buttons that I will place on myView

By accident, I passed the wrong view to fillRects, not myView, as intended. Therefore the UILabel I create below is larger than it should be.

But my understanding was that it should have been cropped since it is a child of myView, which is constrained to the safeAreaLayout guide. Yet from the included image, you can see that it goes beyond myView's area on the screen.

Is my error in the way I applied the safeareaLayout guides? Or my understanding as to how they work?

enter image description here

import UIKit

class MainMenuCtrl: UIViewController {

    var viewBounds          : CGRect = .zero
    var topLabelRect        : CGRect = .zero
    var bottomLabelRect     : CGRect = .zero
    var menuRect            : CGRect = .zero
    
    private let myView : UIView = {
        let myView = UIView()
        myView.translatesAutoresizingMaskIntoConstraints = false
        myView.backgroundColor = .gray
        return myView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Set background color func
        setBGC(vc: view)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        view.backgroundColor = .green
        view.addSubview(myView)
        addContraints(main: view, child: myView)
        

        ////fill the CGRects for all the labels, and buttons
        fillRects(vc: self)
        
        let label = UILabel(frame: self.topLabelRect)
        label.textAlignment = .center
        label.backgroundColor = .red
        label.text = "hello"
        label.textColor = nameColor
        label.font = .systemFont(ofSize: 40)
        label.adjustsFontSizeToFitWidth = true
        label.minimumScaleFactor = 0.7
        
        myView.addSubview(label)
        
    }
    
    override var prefersStatusBarHidden: Bool {
           return false
       }

    override var preferredStatusBarStyle: UIStatusBarStyle {
       return .darkContent
    }
}

Here is the code for fillRects func fillRects (vc: MainMenuCtrl) {

vc.viewBounds = vc.view.frame

vc.topLabelRect = CGRect(x: vc.viewBounds.minX, y: vc.viewBounds.minY,
                         width: vc.viewBounds.width, height: vc.viewBounds.height * 0.05)
vc.bottomLabelRect   = CGRect(x: vc.viewBounds.minX, y: vc.viewBounds.height * 0.9,
                              width: vc.viewBounds.width, height: vc.viewBounds.height * 0.05)
vc.menuRect          = CGRect(x: vc.viewBounds.minX, y: vc.viewBounds.height * 0.2,
                              width: vc.viewBounds.width, height: vc.viewBounds.height * 0.6)

}


Solution

  • A view has a clipToBounds property that dictates whether subViews are restricted to the bounds of their parent view. The default value for this is false, which explains the behaviour you are experiencing.

    Setting view.clipToBounds = true on the parent view should result in the sub view behaving as you expected.