简体   繁体   中英

NSLayoutConstraint - can't set subview frame to parent view bounds

I have a testView UIView and subview named testViewSub. The testView is constrained by using NSLayoutConstraint . And i set subView frame to testView.bounds . But it doesn't work. Here is the code

let testView = UIView()
    testView.backgroundColor = UIColor.red

    self.view.addSubview(testView)

    testView.translatesAutoresizingMaskIntoConstraints = false


    NSLayoutConstraint(item: testView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1.0, constant: 30).isActive = true

    NSLayoutConstraint(item: testView, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1.0, constant: -30).isActive = true

    NSLayoutConstraint(item: testView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1.0, constant: 200).isActive = true

    NSLayoutConstraint(item: testView, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 0.15, constant: 0).isActive = true




    let testViewSub = UIView()
    testViewSub.backgroundColor = UIColor.green
    testViewSub.frame = testView.bounds
    self.testView.addSubview(testViewSub)

    testViewSub.translatesAutoresizingMaskIntoConstraints = false

But if i set testView's frame using CGRect . It works.

Where is the layout happening? I've run into issues before where the constraints don't take effect until the view appears, so relying on frames to be the correct size in viewDidLoad or viewWillAppear causes problems.

In this case, adding testViewSub in viewDidAppear worked for me, though I'm not sure it's the way I would recommend. Using constraints to lay it out, just as with testView , will also work from viewDidLoad or viewWillAppear :

    // layout constraints however you want - in this case they are such that green view's frame = red view's bounds

    let testViewSub = UIView()
    testViewSub.backgroundColor = UIColor.green
    self.testView.addSubview(testViewSub)

    NSLayoutConstraint(item: testViewSub, attribute: .leading, relatedBy: .equal, toItem: testView, attribute: .leading, multiplier: 1.0, constant: 0).isActive = true

    NSLayoutConstraint(item: testViewSub, attribute: .trailing, relatedBy: .equal, toItem: testView, attribute: .trailing, multiplier: 1.0, constant: 0).isActive = true

    NSLayoutConstraint(item: testViewSub, attribute: .top, relatedBy: .equal, toItem: testView, attribute: .top, multiplier: 1.0, constant: 0).isActive = true

    NSLayoutConstraint(item: testViewSub, attribute: .height, relatedBy: .equal, toItem: testView, attribute: .height, multiplier: 1.0, constant: 0).isActive = true


    testViewSub.translatesAutoresizingMaskIntoConstraints = false

This will also deal with rotation better than simply setting the frame.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM