首页 > 解决方案 > 将 searchBar 添加到 tableHeaderView 会增加底部空白

问题描述

我需要补充一点UISearchControllerUIViewController我知道最好的方法是将它添加到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但是,此解决方案在UIViewControllernot the not the athkarSearchTablenot good :/底部添加了一个空白区域,如下面的屏幕截图:

在此处输入图像描述

我试图将页脚设置为空视图并将页脚的高度设置为零,但它们都不起作用:/

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

标签: swiftuitableviewuisearchcontroller

解决方案


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

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

推荐阅读