将searchBar添加到tableHeaderView中会添加底部空白空间

时间:2019-06-09 07:32:24

标签: swift uitableview uisearchcontroller

我需要将UISearchController添加到UIViewController,我知道最好的方法是像这样将其添加到UINavigationController

    let resultSearchController = UISearchController(searchResultsController: athkarSearchTable)
    resultSearchController?.searchResultsUpdater = athkarSearchTable

    navigationItem.searchController = resultSearchController

问题是我不能使用UINavigationController,所以我找到了另一种解决方案:将其添加到tableHeaderView如下:

let resultSearchController = ({
        let controller = UISearchController(searchResultsController: athkarSearchTable)

        controller.searchResultsUpdater = athkarSearchTable
        // to give a semi-transparent background when the search bar is selected.
        controller.dimsBackgroundDuringPresentation = true
        controller.searchBar.sizeToFit()
        controller.searchBar.barStyle = UIBarStyle.default
        controller.searchBar.searchBarStyle = .minimal

        tableView.tableHeaderView = controller.searchBar

        return controller
    })()
    // to limit the overlap area to just the View Controller’s frame instead of the whole Navigation Controller
        definesPresentationContext = true

但是,此解决方案在tableView的{​​{1}}底部而不是UIViewController的底部athkarSearchTable的底部添加了一个空白:

enter image description here

我试图将页脚设置为空视图,并将页脚的高度设置为零,但没有一个起作用:/

任何建议如何删除底部的空白空间?

1 个答案:

答案 0 :(得分:0)

通过添加以下行来解决问题:

tableView.rowHeight          = UITableView.automaticDimension
tableView.estimatedRowHeight = 400