UIStackView Distribution Fill Equally
As a confirmation, this is the current behavior:
And this is required one:
You can follow this simple trick to achieve it:
P.S: I assume that you added the needed appropriate constraints for your stack view.
If your stack view doesn't have a "height" constraint, add one:
Now, add it as an IBOutlet
to the assigned ViewController; In my example, I'm calling it stackHeight
:
@IBOutlet weak var stackHeight: NSLayoutConstraint!
In the event that you want to hide the view (in my example, I'm hiding the orange button based on IBAction
assigned to itself, when tapping on 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
}
If you don't want to remove the bottom constraint, just set distribution as .fill
and add a transparent UIView
at the end of the stack.
This view will stretch while the others will keep stacked on top.
Remove the constraint on the stack view's bottom edge. Then it will resize to just fit the visible controls.