0

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?

Gholamali Irani
  • 4,391
  • 6
  • 28
  • 59
Ivan Kisin
  • 385
  • 1
  • 3
  • 12
  • You better use notification. Check [this](https://stackoverflow.com/questions/41718520/nsnotificationcenter-swift-3-0-on-keyboard-show-and-hide) – Ryan Jan 11 '18 at 20:02
  • 1. Do not hardcode a keyboard height. 2. You never use the `textField` parameter of your `moveTextField` method. 3. Use the block version of UIView animations. It's much easier. – rmaddy Jan 11 '18 at 20:03

2 Answers2

0

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()

         })

    }
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

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)
Eugene Laminskiy
  • 594
  • 2
  • 11