UIView overriding my constraints

I'm new to Xcode and Swift.


I'm trying to go through the sample here: https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ImplementingACustomControl.html#//apple_ref/doc/uid/TP40015214-CH19-SW1


I believe I have my class set up correctly:


class RatingControl: UIStackView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupButtons()
    }
    required init(coder: NSCoder) {
        super.init(coder: coder)
        setupButtons()
    }

    private func setupButtons(){
        let button = UIButton()
        button.backgroundColor = UIColor.red
        button.translatesAutoresizingMaskIntoConstraints = false
        button.heightAnchor.constraint(equalToConstant: 44.0).isActive = true
        button.widthAnchor.constraint(equalToConstant: 44.0).isActive = true
        addArrangedSubview(button)
    }
}


However, when I run the app in Simulator, the UIButton does not have a height and width of 44, but instead takes up the full area of the containing UIStackView.


I can see the confilicting constraints in the output:


"<NSLayoutConstraint:0x60800009fb80 'UIView-Encapsulated-Layout-Height' FoodTracker.RatingControl:0x7fa5d7505560.height == 110 (active)>"

"<NSLayoutConstraint:0x60800009fb30 'UIView-Encapsulated-Layout-Width' FoodTracker.RatingControl:0x7fa5d7505560.width == 200 (active)>"


What are UIView-Encapsulated-Lauout-Height and UIView-Encapsulated-Layout-Width? The guide doesn't mention them. How do I get my programmatically added button to respect the constraints I've given it, rather than these inherited constraints?

It may be a problem of priority in constraints.


Look at answer 76 here :

h ttp://stackoverflow.com/questions/23308400/auto-layout-what-creates-constraints-named-uiview-encapsulated-layout-width-h


Based on a ton of observation I believe (but cannot know for certain) that the constraints named

UIView-Encapsulated-Layout-Width
and
UIView-Encapsulated-Layout-Height
are created by
UICollectionView
and friends, and exist to enforce the size returned by the
sizeForItemAtIndexPath
delegate method. I guess it's there to ensure that the
UICollectionViewCell
set up by
cellForItemAtIndexPath
ends up the size that it was told it would be.

Which answers my initial question here. A second question is why were the constraints unsatisfiable? The cell's intrinsic height should have been the same as

UIView-Encapsulated-Layout-Height
. Again, I don't know for certain, but I suspect it was a rounding error (i.e. intrinsic height came to 200.1 pixels, the
UIView-Encapsulated-Layout-Height
maybe rounded to 200. The fix I came up with was to just lower the priority of the relevant cell constraint to allow
UIView-Encapsulated-Layout-Height
to have the last word.

I was having layout issues until I realized that when adding the Horizontal Stack View to the Main.storyboard, I needed to add it to the existing UIStackView (the one with the label, text field, button, and image). I had initially added it directly to the main View of the storyboard, but that was incorrect.

UIView overriding my constraints
 
 
Q