1

So i have this function to move the TextField in ScrollView but the problem is some TextField on top sometimes move too far when the keyboard is presented

Here is my code :

 func textFieldDidBeginEditing(_ textField: UITextField) {
        moveTextFIeld(textField: textField, moveDistance: -20, up: true)
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        moveTextFIeld(textField: textField, moveDistance: -20, up: false)
    }

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


        tanggalLahirTextField.resignFirstResponder()
}

    func moveTextFIeld(textField:UITextField,moveDistance:Int,up:Bool){
        let moveDuration = 0.3
        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)
//        self.view.frame=CGRect.offsetBy(self.view.frame,0,movement)
        UIView.commitAnimations()


    }
Kaushik Makwana
  • 1,287
  • 2
  • 11
  • 24
Indra Sen
  • 27
  • 1
  • 8
  • 1
    Try [IQKeyboardManager](https://github.com/hackiftekhar/IQKeyboardManager) – Shahrukh May 24 '18 at 07:42
  • yup use any keyboard avoiding lib. like IQWKeyboardManager – Moayad Al kouz May 24 '18 at 07:42
  • Possibly Related: https://stackoverflow.com/questions/26689232/scrollview-and-keyboard-swift https://stackoverflow.com/questions/13161666/how-do-i-scroll-the-uiscrollview-when-the-keyboard-appears?noredirect=1&lq=1 – Ahmad F May 24 '18 at 07:50

1 Answers1

0

we can use IQKeyboardManager from cocoapods.Click here

himaja
  • 1