2015-12-02 61 views

回答

7

'填充比例' 分配型與內在含量的大小工作。因此,如果我們的垂直堆棧(高度爲600)視圖具有2個視圖,ViewA(內在內容高度200)和ViewB(內在內容高度100),堆棧視圖將它們的大小設置爲ViewA(高度400)和ViewB(高度400)身高200)。

此外,

  1. 如果所有的觀點並不具有內在的內容高度,垂直堆棧視圖將始終顯示的IB錯誤「需求約束:Y位置或高度」。
  2. 沒有固有高度的視圖將摺疊到零高度。
  3. 具有固有高度的視圖將按比例分配自己。

你真正想要的是什麼

'填充' 式分佈有兩個約束。

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

這就是所有。希望能幫助到你。

enter image description here

+0

非常感謝你。幫助我更瞭解堆棧視圖 – Lachtan

0

你也可以實現它編程,你可以消除一個文本字段,然後用堆棧視圖的填充同樣分佈將其返回,類似如下:

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 

    } 

} 
相關問題