I'm having trouble with my code. I'm trying to move the UIScrollView
when I'm editing an UITextField
that should be hidden by the keyboard pop.
I'm moving the main frame right now because I don't know how to 'scroll up' in the code.
So, I did a little bit of code, it's working fine but when I edit an UItextfield and I switch to another UITextField
without pressing on the 'return' button the main view goes waaayyyyy to far up.
I did an NSLog()
with my variables size, distance and textFieldRect.origin.y as you can see below. When I put two UITextField
at the same place (y origin) and I do this particular 'switch' (wihtout pressing return) , I get the same numbers, whereas my code worked fine for the first UITextField
editing but not for the second editing.
Check this out:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
{
int size;
CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
size = textFieldRect.origin.y + textFieldRect.size.height;
if (change == FALSE)
{
size = size - distance;
}
if (size < PORTRAIT_KEYBOARD_HEIGHT)
{
distance = 0;
}
else if (size > PORTRAIT_KEYBOARD_HEIGHT)
{
distance = size - PORTRAIT_KEYBOARD_HEIGHT + 5; // +5 px for more visibility
}
NSLog(@"origin %f", textFieldRect.origin.y);
NSLog(@"size %d", size);
NSLog(@"distance %d", distance);
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= distance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
change = FALSE;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
change = TRUE;
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += distance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
Any ideas ?
The recommended way from Apple is to change the
contentInset
of theUIScrollView
. It is a very elegant solution, because you do not have to mess with thecontentSize
. Following code is copied from the Keyboard Programming Guide, where the handling of this issue is explained. You should have a look into it.Swift version:
My solution has 4 step:
- Step 1: the function listens when the keyboard appears
- Step 2: the function listens when the keyboard disappears
- Step 3: add these functions to the notification center:
- Step 4: remove listen when view controller disappers
You can scroll by using the property
contentOffset
inUIScrollView
, e.g.,There's also a method to do animated scrolling.
As for the reason why your second edit is not scrolling correctly, it could be because you seem to assume that a new keyboard will appear every time editing starts. You could try checking if you've already adjusted for the "keyboard" visible position (and likewise check for keyboard visibility at the moment before reverting it).
A better solution might be to listen for the keyboard notification, e.g.:
The only thing I would update in Apple code is the keyboardWillBeHidden: method, to provide smooth transition.
Swift 4 Solution:
I used this answer supplied by Sudheer Palchuri https://stackoverflow.com/users/2873919/sudheer-palchuri https://stackoverflow.com/a/32583809/6193496
In ViewDidLoad, register the notifications:
Add below observer methods which does the automatic scrolling when keyboard appears.