objective-c 如何在堆栈视图中设置容器的高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34049529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set height of containers in stack view?
提问by Lachtan
I'd like to ask you if it's possible to set height in percentage of each container placed in vertical stack view? I want to have 3 containers in stack view. First should take 40% of screen size, second 20% and third 40%. Thank you
我想问你是否可以设置垂直堆栈视图中每个容器的高度百分比?我想在堆栈视图中有 3 个容器。第一个应该占屏幕尺寸的 40%,第二个 20%,第三个 40%。谢谢
回答by BangOperator
'Fill proportionally' distribution type works with intrinsic content size.
“按比例填充”分布类型适用于内在内容大小。
So if our vertical stack(height say 600) view has 2 views, ViewA (intrinsic content height 200) and ViewB(intrinsic content height 100), the stack view will size them to ViewA(height 400) and ViewB(height 200).
因此,如果我们的垂直堆栈(高度为 600)视图有 2 个视图,ViewA(内在内容高度 200)和 ViewB(内在内容高度 100),则堆栈视图会将它们调整为 ViewA(高度 400)和 ViewB(高度 200)。
Also,
还,
- If all the views do not have intrinsic content height, vertical stack view will always show an IB error "Needs constraint for: Y position or Height".
- Views with no intrinsic height will collapse to zero height.
- Views that have intrinsic height will distribute themselves proportionally.
- 如果所有视图都没有固有内容高度,垂直堆栈视图将始终显示 IB 错误“需要约束:Y 位置或高度”。
- 没有固有高度的视图将折叠到零高度。
- 具有固有高度的视图将按比例分布。
What you really want
你真正想要的
is the 'fill' type distributionwith two constraints.
是具有两个约束的“填充”类型分布。
- ViewA.height = 2* ViewB.height
- ViewB.height = 0.5 * ViewC.height
- ViewA.height = 2* ViewB.height
- ViewB.height = 0.5 * ViewC.height
Thats all. Hope it helps.
就这样。希望能帮助到你。
回答by Ahmadiah
You could also implement it programmatically where you could eliminate one text field and then return it back with fill equally distribution of stack view, like the following:
您还可以以编程方式实现它,您可以消除一个文本字段,然后将其返回,并填充堆栈视图的均等分布,如下所示:
class LoginViewController: UIViewController{
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
nameTextField.translatesAutoresizingMaskIntoConstraints = false
emailTextField.translatesAutoresizingMaskIntoConstraints = false
passwordTextField.translatesAutoresizingMaskIntoConstraints = false
}
// IBAction
@IBAction func registerLoginSegmented(_ sender: Any) {
if (sender as AnyObject).selectedSegmentIndex == 0{
// Before we resize (shrink) the nameTextField, change the stackview' distribution from "fill equally" to just "fill"
stackView.distribution = .fill
// Change the nameTextField's text
heightConstraintNameTextField = nameTextField.heightAnchor.constraint(equalToConstant: 0)
heightConstraintNameTextField?.isActive = true
// Rearrange the height of the emailTextField
heightConstraintEmailTextField = emailTextField.heightAnchor.constraint(equalToConstant: 50)
heightConstraintEmailTextField?.isActive = true
// Rearrange the height of the passwordTextField
heightConstraintPasswordTextField = passwordTextField.heightAnchor.constraint(equalToConstant: 50)
heightConstraintPasswordTextField?.isActive = true
}
else {
// Return the nameTextField by simply trun off the constrants and assign "fillEqually" instead of "fill"
heightConstraintNameTextField?.isActive = false
heightConstraintEmailTextField?.isActive = false
heightConstraintPasswordTextField?.isActive = false
stackView.distribution = .fillEqually
}
}


