I currently use this method to scroll the view up and down when I have multiple text fields on the screen and don’t want the keyboard hiding the text field. I found this code online and it works very well for the most part.
In the interface file
@interface ViewController : UIViewController<UITextFieldDelegate>
{
IBOutlet UITextField *textField1;
IBOutlet UITextField *textField2;
IBOutlet UITextField *textField3;
//Float
CGFloat animatedDistance;
}
in the implementation
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
Then I use these methods to make the view scroll up and down when text field is clicked
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
In this example, I have 3 text fields, positioned at top of the screen, middle of the screen and bottom of the screen. I also have this tool bar added to each text field with next previous and done buttons in it. I am sure you can guess what they do.
This code works well except for one issue. If I am editing a text field, and I click on a text field in the middle of the screen, the view scrolls up so the text field isn’t hidden, which is good. However if I then click the home button while in the middle of editing, then access the application again, when the view is opened again, the scroll is reset back to way it normally is, but the textfield is still being edited, so when I do the click the done button, the view acts is if it is still scrolled up, so it scrolls back down again, which causes the view to scroll off the bottom of the screen
I have also tried using notifications for when keyboard is shown or hidden to scroll the view, but same problem happens.
Has anyone had this issue before? And if so, how did they resolve it.
Thanks in advance
Even i got this issue, and fixed like below.
You need to add an observer for
UIApplicationWillResignActiveNotificationin your ViewController where textFields are present.And for that action resign all the keyboards (
resignFirstResponder).This makes your view in right position before app entering background.
And make sure add & remove the notification observers in the right place.
You can add observer in
viewDidLoadand remove observer inviewDidUnLoad.Their might be one more issue when using this view scroll with textFields, if the orientation is supported this issue can occur.
Let me know if you tracked this issue, will help you.