0

While developing an ios application using swift i came across this problem. When the soft keyboard appears i need to adjust the view in order to show the textfield thats getting hidden. The textfield gets hidden in iphone 4s,5,5s. But in iphone 6 and above its perfect even after the keyboard appears. So i want to know the solution for this scenario. Adjust the view only if the textfield/any field gets hidden due to Soft Keyboard, else let it be as it is.

1 Answers1

0

The below code will move your view 200 pixels above when you starts typing. The view will animate back to original position when the textfield ends editing. Do not forget to set the delegates for the textfields.

override func textFieldDidBeginEditing(textField: UITextField) {
    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(0.3)
    UIView.animationBeginsFromCurrentState = TRUE
    self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - 200.0, self.view.frame.size.width, self.view.frame.size.height)
    UIView.commitAnimations()
}


func textFieldDidEndEditing(textField: UITextField) 
{
    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(0.3)
    UIView.animationBeginsFromCurrentState = TRUE
    self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + 200.0, self.view.frame.size.width, self.view.frame.size.height)
    UIView.commitAnimations()
}
Abhinandan Pratap
  • 2,048
  • 1
  • 14
  • 36