UIStackView 分布均匀填充 [英] UIStackView Distribution Fill Equally

查看:88
本文介绍了UIStackView 分布均匀填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个包含四 (4) 个 UIViewUIStackView.如果我删除其中一 (1) 个 UIView,其他三 (3) 个将填充 UIStackView 中的整个空间.

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.

我的问题:

如何在 UIView 上添加最大高度,使其不会填充 UIStackView 的整个空间,即使分布被均匀填充?我读了一些关于添加约束的内容,但我无法让它工作.顺便说一下,我正在使用 swift.

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.

谢谢.

推荐答案

作为确认,这是当前的行为:

As a confirmation, this is the current behavior:

这就是你要求的是:

为了实现它,你可以遵循这个简单的技巧:

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

P.S:我假设您为堆栈视图添加了所需的适当约束.

P.S: I assume that you added the needed appropriate constraints for your stack view.

如果您的堆栈视图没有 "height" 约束,请添加一个:

If your stack view doesn't have a "height" constraint, add one:

现在,将其作为 IBOutlet 添加到指定的 ViewController;在我的示例中,我将其称为 stackHeight:

Now, add it as an IBOutlet to the assigned ViewController; In my example, I'm calling it stackHeight:

@IBOutlet weak var stackHeight: NSLayoutConstraint!

如果您想隐藏视图(在我的示例中,我根据分配给自身的 IBAction 隐藏橙色按钮,当点击它时,应该隐藏),您需要从 stackHeight.constant 中获取要隐藏和减去的视图的高度:

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
    }

这篇关于UIStackView 分布均匀填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆