How to fix position of UIView when keyboard moved up?

How do I fix the position of UIView with image and label when the keyboard is moved up?

I made a code to move up my textField when the keyboard shows, but instead this moves everything.

func textFieldDidEndEditing(_ textField: UITextField) {
    moveTextField(textField: rasstoyanietextField, moveDistance: -215, up: false)
}


func textFieldDidBeginEditing(_ textField: UITextField) {
    moveTextField(textField: rasstoyanietextField, moveDistance: -215, up: true)
}


func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    rasstoyanietextField.resignFirstResponder()

    return true
}


func moveTextField(textField: UITextField, moveDistance: Int, up: Bool){
    let moveDuration = 0.1
    let movement: CGFloat = CGFloat(up ? moveDistance : -moveDistance)

    UIView.beginAnimations("animateTextField", context: nil)
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.setAnimationDuration(moveDuration)
    self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement)
    UIView.commitAnimations()

}

How could I fix it?

2 answers

  • answered 2018-01-11 20:05 Sh_Khan

    Add keybaord observers

      NotificationCenter.default.addObserver(
                    self,
                    selector: #selector(handleKeyboardDidShow),
                    name: NSNotification.Name.UIKeyboardDidShow,
                    object: nil)
    
    
                NotificationCenter.default.addObserver(
                    self,
                    selector: #selector(handleKeyboardWillHide),
                    name:NSNotification.Name.UIKeyboardWillHide,
                    object: nil)
    

    I think it's better to use scrollviews in viewControllers that have textfeilds/textViews to move up all items when showing but here you may Hook the bottom constraint of the textfeild or it's superview and drag it as IBOutlet and do this

     @objc func handleKeyboardDidShow(notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
    
              self.textFeilfBottomCon.constant = -1 * keyboardSize.height
    
              UIView.animate(withDuration: 0.5, animations: {
    
                  self.view.layoutIfNeeded()
    
             })
    
    
        }
    }
    
    @objc func handleKeyboardWillHide(notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
    
                self.textFeilfBottomCon.constant = 0
    
                 UIView.animate(withDuration: 0.5, animations: {
    
                  self.view.layoutIfNeeded()
    
             })
    
        }
    }
    

  • answered 2018-01-11 20:08 Eugene Laminskiy

    Because you move whole view, instead of textview only. Change line:

    self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement)
    

    with:

    textField.frame = textField.frame.offsetBy(dx: 0, dy: movement)