2016-03-16 81 views
0

我試圖居中所有的視圖水平居中和垂直間距爲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> 

我已經居中密碼字段水平和垂直。然後將其用作垂直空間其他視圖的參考。任何指針什麼即時通訊做錯了?

+1

要設置NSLayoutAttribute.CenterXWithinMargins兩個觀點,第一個是passwordTextField和第二個是emailidTextField。 –

回答

0

你的約束工作正常,我有我的代碼試圖相同的,這是工作,你期待,只是我已經增加了一些特性的展示按鈕和文本框,

enter image description here

用於約束的編輯的代碼是如下,

讓emailidTextField =的UITextField() emailidTextField.borderStyle = UITextBorderStyle.Line emailidTextField.translatesAutoresizingMaskIntoConstraints =假 è mailidTextField.placeholder =「電子郵件ID」

let passwordTextField = UITextField() 
    passwordTextField.borderStyle = UITextBorderStyle.Line 
    passwordTextField.translatesAutoresizingMaskIntoConstraints = false 
    passwordTextField.placeholder = "Password" 

    let loginButton = UIButton(type: UIButtonType.System) as UIButton 
    loginButton.translatesAutoresizingMaskIntoConstraints = false 
    loginButton.setTitle("Login", forState: UIControlState.Normal) 

    loginButton.titleLabel?.textColor = UIColor.blueColor() 
    loginButton.backgroundColor = UIColor.lightGrayColor() 

    let signupButton = UIButton(type: UIButtonType.System) as UIButton 
    signupButton.translatesAutoresizingMaskIntoConstraints = false 
    signupButton.setTitle("Sign Up", forState: UIControlState.Normal) 
    loginButton.titleLabel?.textColor = UIColor.blueColor() 
    signupButton.backgroundColor = UIColor.lightGrayColor() 


    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)) 
+0

非常感謝。 – vanquish

+0

乾杯!快樂的編碼。 –