UIStackView Distribution Fill Equally

前端 未结 3 660
醉梦人生
醉梦人生 2020-11-28 14:44

So, I have a UIStackView that contains four (4) UIViews. If I remove one (1) of those UIViews, the other three (3) will fill the entir

相关标签:
3条回答
  • 2020-11-28 14:56

    Remove the constraint on the stack view's bottom edge. Then it will resize to just fit the visible controls.

    0 讨论(0)
  • 2020-11-28 15:11

    Try changing the "Distribution" property in the Attributes inspector of the stack view to Equal spacing. It should work.

    Or you can change it to fill equally to fill the remaining 3 views equally in the Stack view .

    0 讨论(0)
  • 2020-11-28 15:14

    As a confirmation, this is the current behavior:

    And this is what are you asking for is:

    In order to achieve it, you could follow this simple trick:

    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!
    

    On 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
        }
    
    0 讨论(0)
提交回复
热议问题