2017-07-31 51 views
1

我想通過訪問bottomConstraint並將其設置爲等於鍵盤的高度來移動我的視圖。我不知道哪個約束我應該以移動視圖了訪問,當用戶輸入文本,下面是我的兩個函數來顯示和隱藏鍵盤:我想讓這個輸入視圖向上移動,所以當鍵盤出現時它不會消失

// MARK: Scroll View Resize on Keyboard Events 
func setupViewResizerOnKeyboardShown() { 
    NotificationCenter.default.addObserver(self, 
              selector: #selector(keyboardWillShowForResizing), 
              name: Notification.Name.UIKeyboardWillShow, 
              object: nil) 
    NotificationCenter.default.addObserver(self, 
              selector: #selector(keyboardWillHideForResizing), 
              name: Notification.Name.UIKeyboardWillHide, 
              object: nil) 
} 
// MARK: Keyboard Scroll 
func keyboardWillShowForResizing(notification: Notification) { 
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue { 
     // We're not just minusing the kb height from the view height because 
     // the view could already have been resized for the keyboard before 
     // I need to get the bottom constraint and set it equal to keyboardSize.height 

    } else { 
     debugPrint("We're showing the keyboard and either the keyboard size or window is nil: panic widely.") 
    } 
} 
func keyboardWillHideForResizing(notification: Notification) { 
    //commentViewBottomConstraint.constant = 0 
} 

這是我的代碼,我怎麼設置我的所有的文本框組件,包括inputsContainerView觀點:

// MARK: Creation for TextFields 
    let nameTextField: UITextField = { 
     let tf = UITextField() 
     tf.placeholder = "Name" 
     tf.translatesAutoresizingMaskIntoConstraints = false 
     return tf 
    }() 
    let nameSeparatorView: UIView = { 
     let view = UIView() 
     view.backgroundColor = UIColor(r: 220, g: 220, b: 220) 
     view.translatesAutoresizingMaskIntoConstraints = false 
     return view 
    }() 
    let emailTextField: UITextField = { 
     let tf = UITextField() 
     tf.placeholder = "Email" 
     tf.translatesAutoresizingMaskIntoConstraints = false 
     return tf 
    }() 
    let emailSeparatorView: UIView = { 
     let view = UIView() 
     view.backgroundColor = UIColor(r: 220, g: 220, b: 220) 
     view.translatesAutoresizingMaskIntoConstraints = false 
     return view 
    }() 
    let passwordTextField: UITextField = { 
     let tf = UITextField() 
     tf.placeholder = "Password" 
     tf.translatesAutoresizingMaskIntoConstraints = false 
     tf.isSecureTextEntry = true 
     return tf 
    }() 
    let profileImageView: UIImageView = { 
     let imageView = UIImageView() 
     imageView.image = UIImage(named: "") 
     imageView.translatesAutoresizingMaskIntoConstraints = false 
     imageView.contentMode = .scaleAspectFill 
     return imageView 
    }() 
    lazy var loginRegisterSegmentedControl: UISegmentedControl = { 
     let sc = UISegmentedControl(items: ["Login", "Register"]) 
     sc.translatesAutoresizingMaskIntoConstraints = false 
     sc.tintColor = UIColor.white 
     sc.selectedSegmentIndex = 1 
     sc.addTarget(self, action: #selector(handleLoginRegisterChange), for: .valueChanged) 
     return sc 
    }() 
    //MARK: Change Button (Login/Register) 
    func handleLoginRegisterChange() { 
     let title = loginRegisterSegmentedControl.titleForSegment(at: loginRegisterSegmentedControl.selectedSegmentIndex) 
     loginRegisterButton.setTitle(title, for: UIControlState()) 

     // change height of inputContainerView, but how??? 
     inputsContainerViewHeightAnchor?.constant = loginRegisterSegmentedControl.selectedSegmentIndex == 0 ? 100 : 150 

     // change height of nameTextField 
     nameTextFieldHeightAnchor?.isActive = false 
     nameTextFieldHeightAnchor = nameTextField.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: loginRegisterSegmentedControl.selectedSegmentIndex == 0 ? 0 : 1/3) 
     // Hides name placeholder when selected imdex is selected 
     if loginRegisterSegmentedControl.selectedSegmentIndex == 0 { 
      nameTextField.placeholder = "" 
     } 
     else { 
      nameTextField.placeholder = "Username" 
     } 
     nameTextFieldHeightAnchor?.isActive = true 

     emailTextFieldHeightAnchor?.isActive = false 
     emailTextFieldHeightAnchor = emailTextField.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: loginRegisterSegmentedControl.selectedSegmentIndex == 0 ? 1/2 : 1/3) 
     emailTextFieldHeightAnchor?.isActive = true 

     passwordTextFieldHeightAnchor?.isActive = false 
     passwordTextFieldHeightAnchor = passwordTextField.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: loginRegisterSegmentedControl.selectedSegmentIndex == 0 ? 1/2 : 1/3) 
     passwordTextFieldHeightAnchor?.isActive = true 
    } 
    func setupLoginRegisterSegmentedControl() { 
     //need x, y, width, height constraints 
     loginRegisterSegmentedControl.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
     loginRegisterSegmentedControl.bottomAnchor.constraint(equalTo: inputsContainerView.topAnchor, constant: -12).isActive = true 
     loginRegisterSegmentedControl.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor, multiplier: 1).isActive = true 
     loginRegisterSegmentedControl.heightAnchor.constraint(equalToConstant: 36).isActive = true 
    } 
    func setupProfileImageView() { 
     //need x, y, width, height constraints 
     profileImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
     profileImageView.bottomAnchor.constraint(equalTo: loginRegisterSegmentedControl.topAnchor, constant: -12).isActive = true 
     profileImageView.widthAnchor.constraint(equalToConstant: 150).isActive = true 
     profileImageView.heightAnchor.constraint(equalToConstant: 150).isActive = true 
    } 
    var inputsContainerViewHeightAnchor: NSLayoutConstraint? 
    var nameTextFieldHeightAnchor: NSLayoutConstraint? 
    var emailTextFieldHeightAnchor: NSLayoutConstraint? 
    var passwordTextFieldHeightAnchor: NSLayoutConstraint? 

    func setupInputsContainerView() { 
     //need x, y, width, height constraints 
     inputsContainerView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
     inputsContainerView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true 
     inputsContainerView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -24).isActive = true 
     inputsContainerViewHeightAnchor = inputsContainerView.heightAnchor.constraint(equalToConstant: 150) 
     inputsContainerViewHeightAnchor?.isActive = true 

     inputsContainerView.addSubview(nameTextField) 
     inputsContainerView.addSubview(nameSeparatorView) 
     inputsContainerView.addSubview(emailTextField) 
     inputsContainerView.addSubview(emailSeparatorView) 
     inputsContainerView.addSubview(passwordTextField) 

     //need x, y, width, height constraints 
     nameTextField.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor, constant: 12).isActive = true 
     nameTextField.topAnchor.constraint(equalTo: inputsContainerView.topAnchor).isActive = true 

     nameTextField.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor).isActive = true 
     nameTextFieldHeightAnchor = nameTextField.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: 1/3) 
     nameTextFieldHeightAnchor?.isActive = true 

     //need x, y, width, height constraints 
     nameSeparatorView.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor).isActive = true 
     nameSeparatorView.topAnchor.constraint(equalTo: nameTextField.bottomAnchor).isActive = true 
     nameSeparatorView.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor).isActive = true 
     nameSeparatorView.heightAnchor.constraint(equalToConstant: 1).isActive = true 

     //need x, y, width, height constraints 
     emailTextField.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor, constant: 12).isActive = true 
     emailTextField.topAnchor.constraint(equalTo: nameTextField.bottomAnchor).isActive = true 

     emailTextField.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor).isActive = true 

     emailTextFieldHeightAnchor = emailTextField.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: 1/3) 

     emailTextFieldHeightAnchor?.isActive = true 

     //need x, y, width, height constraints 
     emailSeparatorView.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor).isActive = true 
     emailSeparatorView.topAnchor.constraint(equalTo: emailTextField.bottomAnchor).isActive = true 
     emailSeparatorView.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor).isActive = true 
     emailSeparatorView.heightAnchor.constraint(equalToConstant: 1).isActive = true 

     //need x, y, width, height constraints 
     passwordTextField.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor, constant: 12).isActive = true 
     passwordTextField.topAnchor.constraint(equalTo: emailTextField.bottomAnchor).isActive = true 

     passwordTextField.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor).isActive = true 
     passwordTextFieldHeightAnchor = passwordTextField.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: 1/3) 
     passwordTextFieldHeightAnchor?.isActive = true 
    } 
    func setupLoginRegisterButton() { 
     //need x, y, width, height constraints 
     loginRegisterButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
     loginRegisterButton.topAnchor.constraint(equalTo: inputsContainerView.bottomAnchor, constant: 12).isActive = true 
     loginRegisterButton.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor).isActive = true 
     loginRegisterButton.heightAnchor.constraint(equalToConstant: 50).isActive = true 
    } 

回答

0

你inputsContainerView將它與此約束Y位置

inputsContainerView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true 

其中PU在視圖的中心。

沒有底部約束集。

你需要做的第一件事是拯救這個約束在一個屬性(創建VAR inputsYConstraint屬性)

inputsYConstraint = inputsContainerView.centerYAnchor.constraint(equalTo: view.centerYAnchor) 
inputsYConstraint.isActive = true 

做最簡單的事情就是設置一個負常數的約束上移。

或者,您可以降低此約束的優先級,並添加一個底部約束,您可以在鍵盤彈出時更改最小約束。

+0

謝謝你的幫助,你是什麼意思,在約束上設置一個負常數來移動它?我將如何做到這一點?我原本想設置它等於keyboardSize.height ... –

+0

https://developer.apple.com/documentation/appkit/nslayoutconstraint inputsYConstraint的類型是NSLayoutConstraint。它有一個名爲「常量」的屬性,如果設置爲負數(這意味着將它垂直居中減去某個數字),則會將此視圖向上移動。 –

+0

我已創建變量...我將在哪裏創建它=負數? –

相關問題