Welcome everybody! This tutorial is going to show you how to move a textfield whenever the keyboard pops up! One of the biggest problems with textfields is that when the keyboard pops up, the content gets stuck under the keyboard resulting in the total or partial obstruction of the very textfield that your users are trying to write in!! There are several fixes for this problem, but I will only be showing you my version. So feel free to explain any methods that I didn’t in the comments, or email me so I can include it in the tutorial!

The easiest method of un-obstructing the textfield is simply by moving it. Here is what you do: First you need to make sure that the delegate is selected for the textfield in your interface builder file (xib containing textfield). So under the connections tab in your inspector window, make sure that your text field has the delegate set to self. You can also set it in the code under viewdidload as:

Code:
 /* UITextField Name */ .delegate = self;

But you have to make sure you set it in the .h file in between the brackets like this, or else you get an error:

Code:
 ViewController : UIViewController /* apparently this is also html code because it won't show up but it is the "UIText Field Delegate" without the "'s or the spaces. */ {

The next step is to add these methods into your code:

Code:
- (void)textFieldDidBeginEditing:(UITextField *)textField {	

}
- (void)textFieldDidEndEditing:(UITextField *)textField {

}

These methods will go off whenever your textfield has been touched, and when it is done. So now to move it when touched: Go ahead and add this code in the textFieldDidBeginEditing method above:

Code:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDuration:0.5];
	[UIView setAnimationBeginsFromCurrentState:YES];
	textfield.frame = CGRectMake(textfield.frame.origin.x, (textfield.frame.origin.y - 100.0), textfield.frame.size.width, textfield.frame.size.height);
	[UIView commitAnimations];
}

Most of the code here is unnecessary because we are simply moving the textfield’s y origin down 100 points, going up. The UIView animations are simply for visual effect, I personally dislike seeing a textfield appear out of thin air. You can also change the y origin’s movement from 100 to any size amount that you need. So go ahead and try it out to see what happens. You should see a nice textfield moving up every time you click on it to change the text, bravo. Now let’s move it back down when you’re done with it.
Add this to your textFieldDidEndEditing method:

Code:
- (void)textFieldDidEndEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDuration:0.5];
	[UIView setAnimationBeginsFromCurrentState:YES];
	textfield.frame = CGRectMake(textfield.frame.origin.x, (textfield.frame.origin.y + 100.0), textfield.frame.size.width, textfield.frame.size.height);
	[UIView commitAnimations];
}

Easy peasy, looks perfect now doesn’t it? Now what if we had more than 1 textfield what would we do? Try this:

Code:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
		if (textField == textField1) {
			[UIView beginAnimations:nil context:NULL];
			[UIView setAnimationDelegate:self];
			[UIView setAnimationDuration:0.5];
			[UIView setAnimationBeginsFromCurrentState:YES];
			textfield1.frame = CGRectMake(textfield1.frame.origin.x, (textfield1.frame.origin.y - 100.0), textfield1.frame.size.width, textfield1.frame.size.height);
			[UIView commitAnimations];
		} else if (textField == textField2) {
			[UIView beginAnimations:nil context:NULL];
			[UIView setAnimationDelegate:self];
			[UIView setAnimationDuration:0.5];
			[UIView setAnimationBeginsFromCurrentState:YES];
			textfield2.frame = CGRectMake(textfield2.frame.origin.x, (textfield2.frame.origin.y - 100.0), textfield2.frame.size.width, textfield2.frame.size.height);
			[UIView commitAnimations];
		}
	}
	- (void)textFieldDidEndEditing:(UITextField *)textField {
		if (textField == textField1) {
			[UIView beginAnimations:nil context:NULL];
			[UIView setAnimationDelegate:self];
			[UIView setAnimationDuration:0.5];
			[UIView setAnimationBeginsFromCurrentState:YES];
			textfield1.frame = CGRectMake(textfield1.frame.origin.x, (textfield1.frame.origin.y + 100.0), textfield1.frame.size.width, textfield1.frame.size.height);
			[UIView commitAnimations];
		} else if (textField == textField2) {
			[UIView beginAnimations:nil context:NULL];
			[UIView setAnimationDelegate:self];
			[UIView setAnimationDuration:0.5];
			[UIView setAnimationBeginsFromCurrentState:YES];
			textfield2.frame = CGRectMake(textfield2.frame.origin.x, (textfield2.frame.origin.y + 100.0), textfield2.frame.size.width, textfield2.frame.size.height);
			[UIView commitAnimations];
		}

Excellent, now let’s do just one more thing: Let’s have it so that the textfield will return whenever you click somewhere else on the screen. I personally dislike having to put text in a textfield every time before the Done button will show up. So here’s what you do: Simply add this code:

Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	[textfield resignFirstResponder];
}

And.. Wallah that’s it! Now you have a very professional text field that not only moves up and down to avoid being trapped behind the keyboard, it also returns whenever you click out of it. No more having to put unnecessary text in to see the done button!!! Huzzah! Well, that’s all I got for you. Feel free to explain different methods of doing it below, and if you have any questions or concerns at all, feel free to email me at Shmoopiapp@gmail.com