I want to have 7 views with same width, fixed spacing between views and everything that fit the superview. That sounds easy. I know the solution without UIStackView. I thought UIStackView could be simplier (I do the job programmatically and not in storyboard). The code below does not do the job? What do I miss ?
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var horizontalViews:[UIView] = []
// 7 views for the stack
for _ in 0...6 {
let dayView = UIView(frame: .zero)
dayView.backgroundColor = .blue
horizontalViews.append(dayView)
}
let stack = UIStackView(arrangedSubviews: horizontalViews)
stack.axis = .vertical
stack.distribution = .fillEqually
stack.spacing = 10
self.view.addSubview(stack)
// the left and right of the stack is stucked to the controller view
stack.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
stack.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
// vertical size and position of the stack
stack.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
stack.heightAnchor.constraint(equalToConstant: 50).isActive = true
}
}