【问题标题】:Adding searchBar to tableHeaderView adds bottom empty space将 searchBar 添加到 tableHeaderView 会增加底部空白
【发布时间】:2019-06-09 07:32:24
【问题描述】:

我需要将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

但是,此解决方案在UIViewController 中的tableView 底部添加了一个空白空间,而不是athkarSearchTable,这不好:/ 如下截图:

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

有什么建议可以去掉底部的空白吗?

【问题讨论】:

    标签: swift uitableview uisearchcontroller


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      • 1970-01-01
      • 2014-06-14
      • 2012-02-15
      • 1970-01-01
      • 2016-01-13
      相关资源
      最近更新 更多