iPhone Keyboard Hides TextField


 

iPhone Keyboard Hides TextField

Previous example of "UIScrollView" illustrate about the scroll view and how to compound it with UIView.

Previous example of "UIScrollView" illustrate about the scroll view and how to compound it with UIView.

iPhone Keyboard Hides TextField

Previous example of "UIScrollView" illustrate about the scroll view and how to compound it with UIView. But it is not a complete solution, as if your view can have number of elements on it. Suppose you have three or four text field and when you click on the text field, the keyboard appears, but it appears right over the text field!

In most cases of iPhone application, keyboard hides the text fields that restricts a user from viewing it and what they are typing in the field until they returns the keyboard. In such cases, we can slightly slides up the text fields and back down when you are done. But generally user likes to stay at the same location when they are done in cases of large form. But again that depends on your requirement.. otherwise both conditions are possible.

Now let's go through the application code to find out what we have to have in our code to slid up the text field when keypad appears.

First of all create IBOutlet, define their properties and in interface builder connect it with file owner. Do not forget to set the delegate of each text field to file owner. Now, declare

UITextField* activeField;

BOOL keyboardShown;
in viewController.h file. And in the viewcontroller.m file add the given code to it.

- (void)keyboardWasShown:(NSNotification*)aNotification {
if (keyboardShown) return;

NSDictionary* info = [aNotification userInfo];

// Get the size of the keyboard.
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;

// Resize the scroll view (which is the root view of the window)
CGRect viewFrame = [mscrollview frame];
viewFrame.size.height -= keyboardSize.height;
mscrollview.frame = viewFrame;

// Scroll the active text field into view.
CGRect textFieldRect = [activeField frame];
[mscrollview scrollRectToVisible:textFieldRect animated:YES];

keyboardShown = YES;
}

- (void)keyboardWasHidden:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];

// Get the size of the keyboard.
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;

// Reset the height of the scroll view to its original value
CGRect viewFrame = [mscrollview frame];
viewFrame.size.height += keyboardSize.height;
//[scrollview scrollRectToVisible:CGRectMake(0, 0, 5, 5) animated:YES];
mscrollview.frame = viewFrame;

keyboardShown = NO;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
activeField = nil;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}

If you will uncomment the...
[scrollview scrollRectToVisible:CGRectMake(0, 0, 5, 5) animated:YES];

in the "keyboardWasHidden" function.. it will back you down when you are done. .

ScreenShot of the application.

Download code

Ads