I have the following playground and can’t figure out why the ImageView width constraing is not being respected and it fills the remaining space on the StackView
import UIKit
import PlaygroundSupport
let view = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 800))
let imageView = UIView(frame: CGRect.zero)
imageView.backgroundColor = UIColor.red
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.heightAnchor.constraint(equalToConstant: 40).isActive = true
imageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
let navBarHeaderStackView = UIStackView()
navBarHeaderStackView.distribution = .equalCentering
navBarHeaderStackView.alignment = .center
navBarHeaderStackView.axis = .horizontal
navBarHeaderStackView.addArrangedSubview(imageView)
//navBarHeaderStackView.addArrangedSubview(titlesStackView)
view.addSubview(navBarHeaderStackView)
navBarHeaderStackView.translatesAutoresizingMaskIntoConstraints = false
navBarHeaderStackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 50).isActive = true
navBarHeaderStackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 8).isActive = true
navBarHeaderStackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
navBarHeaderStackView.heightAnchor.constraint(equalToConstant: 60).isActive = true
navBarHeaderStackView.backgroundColor = UIColor.blue
PlaygroundPage.current.liveView = view
2
Answers
Because of this constraint
comment it and try
Set
trailingAnchor
tolessThanOrEqualTo
insted ofequalTo
.