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.
Asked
Active
Viewed 122 times
1 Answers
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,142
- 1
- 18
- 39
-
i want to move the textfield only if it hides due to keyboard. In other cases i don't want to. In larger screens like iphone 6 i dont require to. – Prabhakar Patil Apr 26 '16 at 10:58
-
use this code this will works on all devices – Abhinandan Pratap Apr 26 '16 at 10:59
-
pls read the question carefully. – Prabhakar Patil Apr 26 '16 at 11:26
-
why you are not understanding that any code written in xcode is for all the devices there is no such code that only working for some specific device or specific screen sizes. i think you are new in ios development – Abhinandan Pratap Apr 26 '16 at 11:33
-
exactly. I understand that buddy. But i want to move the textview if and only if the textfield hides,as in small screen devices and not for the large screen devices. – Prabhakar Patil Apr 26 '16 at 12:21