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 ?
You actually don't need a UIScrollView to do this. I used this code and it works for me:
I know it's an old question now but i thought it might help others. I wanted something a little easier to implement for a few apps i had, so i made a class for this. You can download it here if you want: https://github.com/sdernley/iOSTextFieldHandler
It's as simple as setting all of the UITextFields to have a delegate of self
And then adding this to your view controller with the name of your scrollView and submit button
Swift 4.2 solution that takes possible heights of UIToolbar and UITabBar into account.
And, if you're targeting < iOS 9, you have to unregister the observer at some point (thanks Joe)
All the answers here seem to forget about landscape possibilities. If you would like this to work when the device is rotated to a landscape view, then you will face problems.
The trick here is that although the view is aware of the orientation, the keyboard is not. This means in Landscape, the keyboards width is actually its height and visa versa.
To modify Apples recommended way of changing the content insets and get it support landscape orientation, I would recommend using the following:
The part to pay attention to here is the following:
What is does, is detects what orientation the device is in. If it is landscape, it will 'swap' the width and height values of the keyboardSize variable to ensure that the correct values are being used in each orientation.
I just implemented this with Swift 2.0 for iOS9 on Xcode 7 (beta 6), works fine here.
Edited for Swift 3
Seems like you only need to set the
contentInset
andscrollIndicatorInset
with Swift 3, the scrolling/contentOffset is done automatically..One of the easiest solutions is using the following protocol:
When you want to use this protocol, you only need to conform to it and assign your scroll view in your controller as follows: