Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

ios - UIStackView Distribution Fill Equally

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 entire space in the UIStackView.

MY QUESTION:

How can I add a max height on a UIView so that it won't fill the entire space of the UIStackView even though the distribution is filled equally? I read something about adding a constraint but I'm not able to make it work. I'm using swift by the way.

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As a confirmation, this is the current behavior:

enter image description here

And this is what are you asking for is:

enter image description here

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:

enter image description here

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
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...