我有一个UIViewController,其中包含带有自定义单元格的UITableView,该单元格内是UILabels,几个不可编辑的UITextView和一个可编辑的UITextView。现在,当我点击靠近表底部或底部的UITextView之一时,UITextView被键盘覆盖。我试过http://cocoawithlove.com/2008/10/sliding-uitextfields-around-to-avoid.html,它非常适合textfield / textview,但不适用于带有自定义单元格的表。任何帮助或建议如何解决?

最佳答案

I fixed the issue. Please see my solution below:

1. First declare a global varibale called "activeFileld"
@property(nonatomic,strong)id activeFiled;

2. Create a method called "registerForKeyboardNotifications"
- (void)registerForKeyboardNotifications
{
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification object:nil]; //Posted immediately prior to the display of the keyboard

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification object:nil]; //Posted immediately prior to the dismissal of the keyboard.
}

3. Called the above method in viewWillAppear:

-(void)viewWillAppear:(BOOL)animated{

  [super viewWillAppear:animated];
  //Register kryboard Notification
   [self registerForKeyboardNotifications];
}
4. Call the Delegate method for UitextFieldd Or UitextView

- (void)textFieldDidBeginEditing:(UITextField *)sender {

        self.activeField = sender;
}
- (void)textFieldDidEndEditing:(UITextField *)sender{
        self.activeField = nil;
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    // save the text view that is being edited
    _notes = textView.text;

}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    // release the selected text view as we don't need it anymore
    _activeField = nil;
}

5.

- (void)keyboardWillShow:(NSNotification *)notification
{

    if([_activeField isKindOfClass:[UITextField class]]) {

        NSDictionary* info = [notification userInfo];
        NSLog(@"Dictionary %@",info);
        CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
        kbRect = [self.view convertRect:kbRect fromView:nil];

        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbRect.size.height, 0.0);
        self.tableView.contentInset = contentInsets;
        self.tableView.scrollIndicatorInsets = contentInsets;

        CGRect aRect = self.view.frame;
        aRect.size.height -= kbRect.size.height;

        UITextField *textField = (UITextField*)_activeField;
        if (!CGRectContainsPoint(aRect, textField.frame.origin) ) {
            [self.tableView scrollRectToVisible:textField.frame animated:YES];
        }
    }else if([_activeField isKindOfClass:[UITextView class]]) {

        NSDictionary* info = [notification userInfo];
        NSLog(@"Dictionary %@",info);
        CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
        kbRect = [self.view convertRect:kbRect fromView:nil];

        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbRect.size.height, 0.0);
        self.tableView.contentInset = contentInsets;
        self.tableView.scrollIndicatorInsets = contentInsets;

        CGRect aRect = self.view.frame;
        aRect.size.height += kbRect.size.height;

        UITextView *activeTextView = (UITextView*)_activeField;
        if (!CGRectContainsPoint(aRect, textField.superview.superview.frame.origin) ) {
            [self.tableView scrollRectToVisible:activeTextView.superview.superview.frame animated:YES];

       }


   }





}

// Called when the UIKeyboardWillHideNotification is received
- (void)keyboardWillHide:(NSNotification *)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    self.tableView.contentInset = contentInsets;
    self.tableView.scrollIndicatorInsets = contentInsets;
}

10-08 00:16