avoid view when opening keyboard from a UITextView inside a UITableView with custom cell

前端 未结 5 943
感动是毒
感动是毒 2021-01-07 09:04

I have a UIViewController that contains a UITableView with custom cells, inside the cell are UILabels, a couple of uneditable UI

5条回答
  •  误落风尘
    2021-01-07 09:38

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

提交回复
热议问题