Вопрос

When i want to put text into my UITextField, keyboard shows at the top of it, and cover the field. How to prevent this? I've just read: this solution but i have no idea where to resize? It is 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 by:

...@"",
 [NSArray arrayWithObjects:
  [TTTableControlItem itemWithCaption:nil control:passwordTextField],          
  nil],...
Это было полезно?

Решение

You just have to implement the delegates of UITextField textFieldDidBeginEditing and textFieldDidEndEditing.Move the frame of parentview up by some value when textFieldDidBeginEditing is called and restore it by same value when delegate method textFieldDidEndEditing is called .

The other way is to add an observer for the UIKeyboardDidShowNotification and move the frame up and move the frame down when UIKeyboardDidHideNotification fires.

Sample :-

[[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; 
} 

Same can br done for textfield delegates.

Другие советы

Resizing is supposed to be applied to the root view: the idea is to set the view frame to match the visible area reduced by keyboard while the first responder is still visible. Check the TTBaseViewController property autoresizesForKeyboard and corresponding 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.
}

For the table view you don't need to change the size, see the keyboardDidAppear implementation of TTTableViewController

///////////////////////////////////////////////////////////////////////////////////////////////////
- (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];
}

you should implement the following method

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

in the implementation you should readjust the textfield frame. if you have other views that you'll want to be shown as well and not hidden by the keyboard you should move them too. it is helpful to have two methods that readjust frames for 2 positions (one when keyboard is shown and one when it's hidden, so once a text field finish to be edited you call the other method in the following text field delegate method:

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

in those two help methods you change frames position and change views to be hidden or not, it will look nicer if you animate 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){
                 }];
     }

I personally highly recommend using TPKeyboardAvoiding by Michael Tyson.

It's very easy to use... (quoting from the Read Me):

For use with UITableViewController classes, drop TPKeyboardAvoidingTableView.m and TPKeyboardAvoidingTableView.h into your project, and make your UITableView a TPKeyboardAvoidingTableView in the xib. If you're not using a xib with your controller, I know of no easy way to make its UITableView a custom class: The path of least resistance is to create a xib for it.

For non-UITableViewControllers, drop the TPKeyboardAvoidingScrollView.m and TPKeyboardAvoidingScrollView.h source files into your project, pop a UIScrollView into your view controller's xib, set the scroll view's class to TPKeyboardAvoidingScrollView, and put all your controls within that scroll view. You can also create it programmatically, without using a xib - just use the TPKeyboardAvoidingScrollView as your top-level view.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top