【问题标题】:Using UISearchController to display search results and keyboard covers bottom rows/sections in UITableView使用 UISearchController 显示搜索结果和键盘覆盖 UITableView 中的底部行/部分
【发布时间】:2017-04-07 15:21:41
【问题描述】:

有一个 UISearchController 在 UITableView 中显示搜索结果,根据联系人姓名分为字母部分和相应的行。

当有一个搜索结果显示几个比 UITableView 大的联系人并显示键盘时,底部的行和部分将被键盘覆盖。

在显示过滤后的搜索结果时增加 UITableView 内容高度的最佳方法是什么,以便底部的联系人可以滚动到用户可见,从而不再被 iOS 键盘覆盖?

我正在使用 Swift 3.1 和 UISearchResultsUpdating 委托和 updateSearchResults 方法来显示过滤结果。

【问题讨论】:

    标签: ios swift uitableview uisearchcontroller


    【解决方案1】:

    你需要注意键盘出现/消失的时候,并相应地设置tableView的contentInset

    在您的 TableViewController 类中创建两个函数作为键盘事件的响应者:

    func keyBoardWillShow(notification: NSNotification) {
        if let keyBoardSize = notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? CGRect {
            let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyBoardSize.height, right: 0)
            self.tableView.contentInset = contentInsets
        }
    }
    
    func keyBoardWillHide(notification: NSNotification) {
        self.tableView.contentInset = UIEdgeInsets.zero
    }
    

    并在ViewDidLoaddeinit 中注册/注销响应者:

    override func viewDidLoad() {
        super.viewDidLoad()
        ...
    
        // register the responders
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyBoardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyBoardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    

    【讨论】:

      【解决方案2】:

      适用于 Swift 4.2 及更高版本。 注册一个观察者。

      override func viewDidLoad() {
          super.viewDidLoad()
          // register the responders
          NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillShow), name: UIResponder.keyboardWillShowNotification , object: nil)
          NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
      }
      

      从 iOS 9(和 OS X 10.11)开始,如果您不使用基于块的观察者,则不需要自己删除观察者。系统会为你做这件事,因为它会尽可能地为观察者使用归零弱引用。

      响应事件的函数。

      @objc func keyBoardWillShow(notification: NSNotification) {
          if let keyBoardSize = notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? CGRect {
              let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyBoardSize.height, right: 0)
              self.tableView.contentInset = contentInsets
          }
      }
      
      @objc func keyBoardWillHide(notification: NSNotification) {
          self.tableView.contentInset = UIEdgeInsets.zero
      }
      

      【讨论】:

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