【问题标题】:How to set height of containers in stack view?如何在堆栈视图中设置容器的高度?
【发布时间】:2016-03-07 02:16:46
【问题描述】:

我想问你是否可以设置垂直堆栈视图中每个容器的百分比高度?我想在堆栈视图中有 3 个容器。第一个应该占屏幕尺寸的 40%,第二个占 20%,第三个占 40%。谢谢

【问题讨论】:

    标签: objective-c swift autolayout uicontainerview uistackview


    【解决方案1】:

    您也可以通过编程方式实现它,您可以消除一个文本字段,然后将其返回并填充平均分布的堆栈视图,如下所示:

    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
    
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      “按比例填充”分布类型适用于固有内容大小

      因此,如果我们的垂直堆栈(高度为 600)视图有 2 个视图,ViewA(固有内容高度 200)和 ViewB(固有内容高度 100),堆栈视图会将它们的大小调整为 ViewA(高度 400)和 ViewB(高度200)。

      还有,

      1. 如果所有视图都没有固有内容高度,垂直堆栈视图将始终显示 IB 错误“需要约束:Y 位置或高度”。
      2. 没有固有高度的视图将折叠到零高度。
      3. 具有固有高度的视图将按比例分布。

      你真正想要的

      是具有两个约束的'fill'类型分布

      1. ViewA.height = 2* ViewB.height
      2. ViewB.height = 0.5 * ViewC.height

      仅此而已。希望对您有所帮助。

      【讨论】:

      • 非常感谢。帮助我更了解堆栈视图
      • 好一个。值得一提的是如何添加高度约束。在 2 个视图之间拖动,选择等高。然后在尺寸检查器中,更改视图之间的乘数
      • 正在使用此方法在 xcode 11 中是否仍然可用?因为我无法按照答案中的描述添加约束
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多