UIStackView分配同样填写 [英] UIStackView Distribution Fill Equally
问题描述
所以,我有一个 UIStackView
,它包含四(4) UIView
。如果我删除那些 UIView
中的一(1)个,其他三个(3)将填充 UIStackView $ c $中的整个空格c>。
So, I have a UIStackView
that contains four (4) UIView
s. If I remove one (1) of those UIView
s, 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.
谢谢。
推荐答案
<如果我没有误会,你面临的是:
If I'm not mistaking, what are you facing is:
您要求的是:
好吧,如果那是你的情况 - 我希望它是 - ,我通过做一个非常简单的把戏来实现这个目的:
Well, if that's your case -and I hope it is-, I achieved this by doing a pretty simple trick:
PS :我假设你为stackView添加了approrpriate约束。
P.S: I assume that you added the approrpriate constraints for your stackView.
如果你的stackView没有height约束,添加一个:
If your stackView 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 besed on IBAction assinged 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屋!