UIStackView Distribution Fill Equally(UIStackView 分布填充均等)
问题描述
所以,我有一个 UIStackView,其中包含四 (4) 个 UIView.如果我删除其中一 (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: 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 分布填充均等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIStackView 分布填充均等
基础教程推荐
- AdMob 广告未在模拟器中显示 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
