UIStackView distribution distribution evenly

So, I have a UIStackView that contains four (4) UIView s. If I remove one (1) from these UIView s, the remaining three (3) will fill all the space in the UIStackView .

MY QUESTION:

How to add maximum height on a UIView so that it does not fill the entire UIStackView space, even if the distribution is filled the same? I read something about adding a limit, but I can't get it to work. By the way, I use swift.

Thanks.

+11
ios autolayout swift constraints uistackview
source share
3 answers

As a confirmation, this is the current behavior:

enter image description here

And this is what you ask for:

enter image description here

To achieve this, you can take advantage of this simple trick:

PS: I assume that you have added the necessary restrictions for your stack view.

If your stack view has no height limit, add it:

enter image description here

Now add it as an IBOutlet to the designated ViewController; In my example, I call it stackHeight :

 @IBOutlet weak var stackHeight: NSLayoutConstraint! 

In case you want to hide the view (in my example, I hide the orange button based on the IBAction assigned to it, when I touch it, it should be hidden), you need to get the height of the view that you want to hide and subtract from stackHeight.constant :

  @IBAction func orangeTapped(_ sender: AnyObject) { orange.isHidden = true // here we go: stackHeight.constant = stackHeight.constant - orange.frame.size.height } 
+14
source share

Remove the restriction on the bottom edge of the stack view. Then it will resize to just fit the visible controls.

+2
source share

Try changing the Distribution property in the stack view attribute inspector to an equal interval. It should work.

Or you can change it to fill the same way to evenly fill in the remaining 3 views in the Stack view.

0
source share

All Articles