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

前端 未结 5 946
感动是毒
感动是毒 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:57

    When your table view contains data entry fields like a UITextField or a UITextView and the table view is long enough to cover the screen, you will have a problem accessing data entry fields that are hidden by the keyboard.
    To overcome this problem two solutions are:

    1. The easiest and recommended way is to use a UITableViewController instead of UIViewController, which automatic make sure keypad won't hide the editable field (If possible use this approach to avoid U.I. adjustment inconvenience)

    2. If you use a UIViewController and a UITableView as its subview. You can scroll your UI’s frame by observing the UIKeyboardWillShowNotification and UIKeyboardWillHideNotification

      - (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.
      }
      
      
      - (void)keyboardWillShow:(NSNotification *)aNotification 
      {  
          CGRect keyboardBounds = [[[aNotification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
      
          [UIView beginAnimations:nil context:nil];
          [UIView setAnimationBeginsFromCurrentState:YES];
      
          self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0); //when keyboard is up, that time just bring your text filed above the keyboard
          self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0);
      
          [self.tableView scrollToRowAtIndexPath:[self findIndexPathToScroll]
                                atScrollPosition:UITableViewScrollPositionTop
                                        animated:YES]; //findIndexPathToScroll implementation not shown
          [UIView commitAnimations];
      }
      
      - (void)keyboardWillHide:(NSNotification *)aNotification 
      {
          [UIView beginAnimations:nil context:nil];
          [UIView setAnimationBeginsFromCurrentState:YES];
          self.tableView.contentInset = UIEdgeInsetsZero; //Once keyboard is hidden then bring back your table into your original position.
          self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero;
          [UIView commitAnimations];
      }
      
      • registerForKeyboardNotifications - call this method when you load the UITableView, ie: viewDidLoad

      • findIndexPathToScroll - (Implementation not shown) Its your business logic to prepare IndexPath where table view should scroll

      • removeObserver 'UIKeyboardWillShowNotification' and 'UIKeyboardWillHideNotification' both in dealloc and viewDidUnload

提交回复
热议问题