我試圖居中所有的視圖水平居中和垂直間距爲40.電子郵件和密碼字段顯示。但是,登錄和註冊按鈕根本不顯示。IOS Swift NSLayoutConstraint。無法設置水平居中的視圖
view.addSubview(emailidTextField)
view.addSubview(passwordTextField)
view.addSubview(loginButton)
view.addSubview(signupButton)
passwordTextField.addConstraint(NSLayoutConstraint(item: passwordTextField, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: self.view.frame.width - 20))
passwordTextField.addConstraint(NSLayoutConstraint(item: passwordTextField, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40))
emailidTextField.addConstraint(NSLayoutConstraint(item: emailidTextField, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: self.view.frame.width - 20))
emailidTextField.addConstraint(NSLayoutConstraint(item: emailidTextField, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40))
loginButton.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: self.view.frame.width - 20))
loginButton.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40))
signupButton.addConstraint(NSLayoutConstraint(item: signupButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: self.view.frame.width - 20))
signupButton.addConstraint(NSLayoutConstraint(item: signupButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40))
view.addConstraint(NSLayoutConstraint(item: passwordTextField, attribute: NSLayoutAttribute.CenterXWithinMargins, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterXWithinMargins, multiplier: 1, constant: 0))
view.addConstraint(NSLayoutConstraint(item: passwordTextField, attribute: NSLayoutAttribute.CenterYWithinMargins, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterYWithinMargins, multiplier: 1, constant: 0))
view.addConstraint(NSLayoutConstraint(item: emailidTextField, attribute: NSLayoutAttribute.CenterXWithinMargins, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterXWithinMargins, multiplier: 1, constant: 0))
view.addConstraint(NSLayoutConstraint(item: emailidTextField, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: passwordTextField, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: -10))
view.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
view.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: passwordTextField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 40))
view.addConstraint(NSLayoutConstraint(item: signupButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
view.addConstraint(NSLayoutConstraint(item: signupButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: loginButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 40))
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7989a9e0 UIButton:0x79990040'Signup'.centerX == UIView:0x79c87d70.centerX>
我已經居中密碼字段水平和垂直。然後將其用作垂直空間其他視圖的參考。任何指針什麼即時通訊做錯了?
要設置NSLayoutAttribute.CenterXWithinMargins兩個觀點,第一個是passwordTextField和第二個是emailidTextField。 –