所以,我有一个 UIStackView
,其中包含四 (4) 个 UIView
.如果我删除其中一 (1) 个 UIView
,其他三 (3) 个将填满 UIStackView
中的整个空间.
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.
谢谢.
作为确认,这是当前的行为:
As a confirmation, this is the current behavior:
这就是你要的:
为了实现它,你可以遵循这个简单的技巧:
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.
如果您的堆栈视图没有 "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 分布填充均等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!