【问题标题】:UISearchController doesn't move my UITableView upUISearchController 不会向上移动我的 UITableView
【发布时间】:2015-04-28 10:03:53
【问题描述】:

如您所见,我的UITableView 的滚动条最大向下,但我的表格不完全可见。为什么?我用UITableViewController

【问题讨论】:

  • 一出现键盘就设置 `tableView.contentInset = UIEdgeInsetsMake(10, 0, 200, 0);` 键盘消失后设置 `table.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);`
  • 你知道,如何获取键盘的高度吗?因为两个方向的高度不同。不使用通知?是否可以使用当前可见的键盘?

标签: ios swift uitableview cocoa-touch uisearchcontroller


【解决方案1】:

Ortwin 答案的好选择是使用UIScrollViewkeyboardDismissMode 属性:

tableView.keyboardDismissMode = .OnDrag

斯威夫特 4.1

tableView.keyboardDismissMode = .onDrag

【讨论】:

    【解决方案2】:

    我建议在用户开始滚动后立即隐藏键盘。 Apple 在他们自己的应用程序中实现了这种行为。为此,请添加您的表格视图控制器:

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

    这需要使用搜索栏填充的searchBar 属性。

    【讨论】:

      【解决方案3】:

      使用通知可能是获取键盘高度的最简单方法,因为我评论设置contentInset 属性以滚动键盘上方的表格视图,一旦它隐藏设置为零昆虫,

      - (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);
        }];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-12
        相关资源
        最近更新 更多