当从UITextView中的UITableView中的自定义单元格中打开键盘时,避免查看。[英] avoid view when opening keyboard from a UITextView inside a UITableView with custom cell

本文是小编为大家收集整理的关于当从UITextView中的UITableView中的自定义单元格中打开键盘时,避免查看。的处理方法,想解了当从UITextView中的UITableView中的自定义单元格中打开键盘时,避免查看。的问题怎么解决?当从UITextView中的UITableView中的自定义单元格中打开键盘时,避免查看。问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

我有一个 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;
}

本文地址:https://www.itbaoku.cn/post/924565.html