Keyboard hides input field

When I want to put text in my UITextField, the keyboard shows at the top and closes the field. How to prevent this? I just read: this is a solution , but I have no idea where to resize? Is this a good way?

My code is:

UITextField * passwordTextField = [[[UITextField alloc] init] autorelease]; passwordTextField.keyboardType = UIKeyboardTypeDefault; passwordTextField.delegate = self; passwordTextField.placeholder = NSLocalizedString(@"somestr", @""); passwordTextField.secureTextEntry = YES; [_controls setObject:passwordTextField forKey:keyPassword]; 

And then I show it in Three20:

 ...@ "", [NSArray arrayWithObjects: [TTTableControlItem itemWithCaption:nil control:passwordTextField], nil],... 
+1
source share
4 answers

You just need to implement the UITextField delegates textFieldDidBeginEditing and textFieldDidEndEditing. Move the frame of the parent view to some value when textFieldDidBeginEditing is called and restore it with the same value when the delegate method textFieldDidEndEditing is called.

Another way is to add an observer for the UIKeyboardDidShowNotification and move the frame up and move the frame down when starting UIKeyboardDidHideNotification.

Example: -

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:)name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardDidHideNotification object:nil]; 

and change the frames in the selectors: -

 - (void)keyboardDidShow:(NSNotification *)aNotification { if ( keyBDidShow) return; NSTimeInterval animationDuration = 0.300000011920929; CGRect frame = self.view.frame; frame.origin.y -= 60; [UIView beginAnimations:@"Rollback" context:nil]; [UIView setAnimationDuration:animationDuration]; self.view.frame = frame; [UIView commitAnimations]; viewUp= YES; } keyBDidShow= YES; } - (void)keyboardWasHidden:(NSNotification *)aNotification { if ( viewUp) { NSTimeInterval animationDuration = 0.300000011920929; CGRect frame = self.view.frame; frame.origin.y += 60; [UIView beginAnimations:@"Rollback" context:nil]; [UIView setAnimationDuration:animationDuration]; self.view.frame = frame; [UIView commitAnimations]; viewUp = NO; } keyBDidShow= NO; } 

The same thing can be done for text field delegates.

+3
source

Resizing is supposed to be applied to the root view: the idea is to set the view frame to fit in the visible area reduced by the keyboard, while the first responder is still visible. Check the TTBaseViewController autoresizesForKeyboard property and related methods

 /////////////////////////////////////////////////////////////////////////////////////////////////// - (void)keyboardWillAppear:(BOOL)animated withBounds:(CGRect)bounds { // Empty default implementation. } /////////////////////////////////////////////////////////////////////////////////////////////////// - (void)keyboardWillDisappear:(BOOL)animated withBounds:(CGRect)bounds { // Empty default implementation. } /////////////////////////////////////////////////////////////////////////////////////////////////// - (void)keyboardDidAppear:(BOOL)animated withBounds:(CGRect)bounds { // Empty default implementation. } /////////////////////////////////////////////////////////////////////////////////////////////////// - (void)keyboardDidDisappear:(BOOL)animated withBounds:(CGRect)bounds { // Empty default implementation. } 

You do not need to resize to represent the table; see keyboardDidAppear TTTableViewController implementation

 /////////////////////////////////////////////////////////////////////////////////////////////////// - (void)keyboardDidAppear:(BOOL)animated withBounds:(CGRect)bounds { [super keyboardDidAppear:animated withBounds:bounds]; self.tableView.frame = TTRectContract(self.tableView.frame, 0, bounds.size.height); [self.tableView scrollFirstResponderIntoView]; [self layoutOverlayView]; [self layoutBannerView]; } 
0
source

you must implement the following method

  - (BOOL) textFieldShouldBeginEditing:(UITextField *)textField{ [self adjustFramesWhenKeyoradIsShown]; } 

in the implementation, you must adjust the text box frame. if you have other views that you want to show, as well as not hidden on the keyboard, you should also move them. It is useful to have two methods that reconfigure frames for two positions (one when the keyboard is displayed, and one when it is hidden, therefore, as soon as editing the text field is edited, you call another method in the following delegation method of the text field:

  - (BOOL)textFieldShouldReturn:(UITextField *)textField { [self adjustFramesWhenKeyoradIsHidden]; } 

in these two help methods you change the position of the frame and change the view to be hidden or not, it will look better if you revive them, something like this:

  - (void) adjustFramesWhenKeyoradIsShown{ [UIView animateWithDuration:0.3 delay:0.0 options: UIViewAnimationCurveEaseOut animations:^{ logoArea.hidden = YES; loginFieldsArea.frame = CGRectMake(0, 0, 320, 250); } completion:^(BOOL finished){ }]; } 
-one
source

I personally recommend using Michael Tyson's TPKeyboardAvoiding .

It is very easy to use ... (to quote from Read Me):

To use with the UITableViewController classes, select TPKeyboardAvoidingTableView.m and TPKeyboardAvoidingTableView.h in your project and make your UITableView TPKeyboardAvoidingTableView in xib. If you are not using xib with a controller, I know there is no easy way to make your UITableView a regular class: the path of least Resistance is to create xib for it.

For non-strong> UITableViewControllers, release TPKeyboardAvoidingScrollView.m and TPKeyboardAvoidingScrollView.h the source files in your project, enter the UIScrollView in your xib controller view, set the scroll view class to TPKeyboardAvoidingScrollView and put all your controls in this scroll view. You can also create it programmatically, without using xib - just use TPKeyboardAvoidingScrollView as your top-level view.

-2
source

All Articles