UISearchController doesn't move my UITableView up

送分小仙女□ 提交于 2019-12-08 02:56:18

问题


As you can see, my UITableView's scroll is maximum down, but my table is not fully visible. Why? I use UITableViewController


回答1:


Good alternative to Ortwin's answer is to use keyboardDismissMode property of UIScrollView:

tableView.keyboardDismissMode = .OnDrag

Swift 4.1

tableView.keyboardDismissMode = .onDrag



回答2:


I recommend to simply hide the keyboard as soon as the user starts scrolling. Apple implements this behavior in their own apps. To do that, add in your table view controller:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.searchBar resignFirstResponder];
}

This requires a searchBar property that is populated with the search bar.




回答3:


using notifications might be easiest way to get the keyboard height as i commented set contentInset property to scroll the tableview above the keyboard, once it hides set to zero insects,

  - (void)viewWillAppear:(BOOL)animate {
    [super viewWillAppear:animate];
   // Do any additional setup after loading the view.
   [[NSNotificationCenter defaultCenter] addObserver:self 
           selector:@selector(keyboardShown:) 
                    name:UIKeyboardWillShowNotification object:nil];
   [[NSNotificationCenter defaultCenter] addObserver:self
          selector:@selector(keyboardHidden:)
                    name:UIKeyboardWillHideNotification object:nil];
   }

 - (void)viewWillDisappear:(BOOL)animated
   {
       //remove notifications
   }

- (void)keyboardShown:(NSNotification*)aNotification
  {
    NSDictionary* info = [aNotification userInfo];
    CGRect keyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

    float height = keyboardRect.size.height;
    tableView.contentInset = UIEdgeInsetsMake(10, 0, height, 0); 
  }


  - (void)keyboardHidden:(NSNotification*)aNotification
  {
      [UIView animateWithDuration:0.2 animations:^{
        table.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
      }];
  }


来源:https://stackoverflow.com/questions/29916323/uisearchcontroller-doesnt-move-my-uitableview-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!