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 ?
For this stuff no need to lot's of coding it's very easy like below code:-
your all textfiled in UIScrollview from nib like this image:-
YourViewController.h
connect IBOutlet from nib and also Connect each delegate of UItextfiled and scrollview delegate from NIB
NOTE if Text-filed delegate not connected then no one method working please insure that all iBOulate and delegate connected correctly
The following is my solutions which works ( 5 steps )
Step1: Add an observer to catch which UITEXTFIELD or UITEXTVIEW ShoudBeginEditing ( where object is inited or ViewDidLoad.
Step2: Post a notification when ..ShouldBeginEditing with OBJECT of UITEXTFIELD or UITEXTVIEW
Step3: The method that (Step1 calles ) assigns the current UITEXTFIELD or UITEXTVIEW
Step4: Add Keyboard observer UIKeyboardWillShowNotification ( same place as Step1 )
and method:
Step5: Add Keyboard observer UIKeyboardWillHideNotification ( same place as step 1 )
and method:
Remember to remove observers!
This is the final code with improvements in Swift
I've found the above answers have been outdated. Also not perfect when scroll.
Here's a swift version.
It will scroll right below the textField, no spare space. And It will restore to the way it was like its first appear.