How to add a view limit inside the stack

I have one stack view which contains 4 buttons. And I add each button to it as well. The mobility of these 4 buttons, I am trying to program to add a constraint to it. Some limitation like .Trailing .Leading .Top .Bottom I can't add error limitation and stack view problem to it. As any solution add these constraints to subview for stackview. if i have a sample it is really good for me. thanks in advance

+3


source to share


1 answer


The power of UIStackView is to reduce the use of constraints, just give it some customization information like axis , distribution, alignment, spacing . the stack will automatically lay out your subview element, since the stack view size is based on its inline subviews, you can set the size of the subview with additional constraints to override.

Adding constraints to a subview stackView is similar to other items in UIView. but not the StackView way and you have to take care of adding conflict constraints.



hope this code demo helps:

let stackView = UIStackView()
let demoView = UIView()
demoView.backgroundColor = UIColor.red

stackView.addArrangedSubview(demoView)
demoView.translatesAutoresizingMaskIntoConstraints = false

// add your constraints as usual
demoView.widthAnchor.constraint(equalToConstant: 300).isActive = true
demoView.heightAnchor.constraint(equalToConstant: 200).isActive = true
demoView.trailingAnchor.constraint(equalTo: stackView.trailingAnchor).isActive = true
demoView.topAnchor.constraint(equalTo: stackView.topAnchor).isActive = true

view.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
stackView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true

      

+2


source







All Articles