【发布时间】:2016-03-07 02:16:46
【问题描述】:
【问题讨论】:
标签: objective-c swift autolayout uicontainerview uistackview
【问题讨论】:
标签: objective-c swift autolayout uicontainerview uistackview
您也可以通过编程方式实现它,您可以消除一个文本字段,然后将其返回并填充平均分布的堆栈视图,如下所示:
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
}
}
【讨论】:
“按比例填充”分布类型适用于固有内容大小。
因此,如果我们的垂直堆栈(高度为 600)视图有 2 个视图,ViewA(固有内容高度 200)和 ViewB(固有内容高度 100),堆栈视图会将它们的大小调整为 ViewA(高度 400)和 ViewB(高度200)。
还有,
你真正想要的
是具有两个约束的'fill'类型分布。
仅此而已。希望对您有所帮助。
【讨论】: