删除UISearchController和UITableViewController

时间:2017-09-23 11:28:33

标签: ios swift uitableview uisearchcontroller

我有一个非常简单的ViewController(嵌入在NavigationController中)。从NavigationBar中,您可以打开UISearchController来查找一些信息。 搜索结果是一个非常简单的TableViewController。

当显示UISearchController时,SearchController和TableViewController之间会有一些额外的空间。它看起来像状态栏的高度是20像素。

如何删除这个额外的空间?

查看截图。

enter image description here enter image description here

这里是ViewController的代码

class ViewController: UIViewController {

    var theSearchController:UISearchController?


    @IBAction func openSearchController(_ sender: UIBarButtonItem) {
        showSearchController()
    }

    /// Display the search controller used to look for a POI, address, ...
    func showSearchController() {
        let mySearchController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SearchControllerId") as! SearchTableViewController

        theSearchController = UISearchController(searchResultsController: mySearchController)

        // Configure the UISearchController
        theSearchController!.searchResultsUpdater = self
        theSearchController!.delegate = self

        theSearchController!.searchBar.sizeToFit()
        theSearchController!.searchBar.delegate = self
        theSearchController!.searchBar.placeholder = NSLocalizedString("MapSearchPlaceHolder", comment: "")
        theSearchController!.hidesNavigationBarDuringPresentation = true
        theSearchController!.dimsBackgroundDuringPresentation = true

        present(theSearchController!, animated: true, completion: nil)

    }

}

extension ViewController: UISearchResultsUpdating, UISearchBarDelegate, UISearchControllerDelegate {
    func updateSearchResults(for searchController: UISearchController) {
        let mySearchController = searchController.searchResultsController as! SearchTableViewController
        mySearchController.reloadAll()
    }
}

这里是TableViewController的代码

class SearchTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        // Mandatory to make sure the TableView is displayed when the search field is empty
        // when user touch it.
        view.isHidden = false
    }


    func reloadAll() {
        view.isHidden = false
        tableView.reloadData()
    }
    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {

        return 2
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "Section \(section)"
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "testCellId", for: indexPath)

        cell.textLabel?.text = "text \(indexPath.section) / \(indexPath.row)"

        return cell
    }
}

修改

Rizvan Rzayev的回答解决了UISearchController和UITableviewController之间的空间问题。 接下来,仍然存在一个问题,因为桌面视图的底部被键盘隐藏,然后无法显示某些行。

要解决此键盘问题和初始问题,请输入完整代码:

import UIKit

class SearchTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        registerKeyboardNotifications()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        // Mandatory to make sure the TableView is displayed when the search field is empty
        // when user touch it.
        view.isHidden = false
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

    }

    deinit {
        unregisterKeyboardNotifications()
    }

    func reloadAll() {
        view.isHidden = false
        tableView.reloadData()
    }
    // MARK: - Table view data source

    func unregisterKeyboardNotifications() {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    }

    func registerKeyboardNotifications() {
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(SearchTableViewController.keyboardWillShow(_:)),
                                               name: NSNotification.Name.UIKeyboardWillShow,
                                               object: nil)

        NotificationCenter.default.addObserver(self,
                                               selector: #selector(SearchTableViewController.keyboardWillHide(_:)),
                                               name: NSNotification.Name.UIKeyboardWillHide,
                                               object: nil)
    }

    var keyboardHeight:CGFloat = 0.0

    @objc func keyboardWillShow(_ notification:Notification) {
        // Extract the Keyboard size

        let info = notification.userInfo! as NSDictionary
        let valueHeight = info.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
        let keyboardForHeight = valueHeight.cgRectValue.size
        keyboardHeight = keyboardForHeight.height
        let contentInsets = UIEdgeInsetsMake(40, 0.0, keyboardForHeight.height, 0.0)
        tableView.contentInset = contentInsets
        tableView.scrollIndicatorInsets = contentInsets
    }


    @objc func keyboardWillHide(_ notification:Notification) {
        tableView.contentInset = .zero
        tableView.scrollIndicatorInsets = .zero
    }


    override func numberOfSections(in tableView: UITableView) -> Int {

        return 2
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "Section \(section)"
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "testCellId", for: indexPath)

        cell.textLabel?.text = "text \(indexPath.section) / \(indexPath.row)"

        return cell
    }

    override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        tableView.contentInset = UIEdgeInsets(top: 40, left: 0, bottom: keyboardHeight, right: 0)
        tableView.scrollIndicatorInsets = tableView.contentInset
    }
}

1 个答案:

答案 0 :(得分:0)

试试吧!

    override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    tableView.contentInset = UIEdgeInsets(top: 65, left: 0, bottom: 0, right: 0)

}