如何将 UITableView 的顶部粘贴到 UISearchBar 的底部?

Posted

技术标签:

【中文标题】如何将 UITableView 的顶部粘贴到 UISearchBar 的底部?【英文标题】:How to stick top of the UITableView to the bottom of the UISearchBar? 【发布时间】:2018-09-12 16:17:45 【问题描述】:

我有一个 UTableView:

tableView = UITableView()
        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "Cell")
        tableView.rowHeight = 60.0
        tableView.tableFooterView = UIView()
        view.addSubview(tableView)

        tableView.translatesAutoresizingMaskIntoConstraints = false

        tableViewHeightAnchor = tableView.heightAnchor.constraint(equalToConstant: 0)
        let constraints = [tableView.topAnchor.constraint(equalTo: view.topAnchor), tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor), tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor), tableViewHeightAnchor!]
        NSLayoutConstraint.activate(constraints)

我的搜索栏是:

    searchController = UISearchController(searchResultsController: nil)
    searchController.searchResultsUpdater = self
    searchController.searchBar.delegate = self
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search for a cell"
    navigationItem.searchController = searchController
    definesPresentationContext = true

但我希望将我的***锚设置为:

tableView.topAnchor.constraint(equalTo: searchController.searchBar.bottomAnchor)

但不是:

tableView.topAnchor.constraint(equalTo: view.topAnchor)

但是当我将它粘贴到 searchBar 的底部锚点时,应用程序会因为层次结构不同而崩溃。

'Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x6000017a6cc0 "UITableView:0x7f81c4876e00.top"> and <NSLayoutYAxisAnchor:0x6000017a6dc0 "_UISearchControllerView:0x7f81c2d3fa30.bottom"> because they have no common ancestor.  Does the constraint or its anchors reference items in different view hierarchies?  That's illegal.'

我该如何解决这个问题? 我试图解决这个问题已经 3 天了,但没有成功

【问题讨论】:

你能展示一下你想要达到的目标吗?如果您将搜索栏放入导航栏(如您的示例中),则将表格视图固定到视图是正确的。 【参考方案1】:

选项A(导航栏中的搜索栏):

let tableView = UITableView()
view.addSubview(tableView)

let searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController

definesPresentationContext = true

tableView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
    tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
    tableView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
    tableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
    tableView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
])

选项 B(搜索栏作为表头视图):

let tableView = UITableView()
view.addSubview(tableView)

let searchController = UISearchController(searchResultsController: nil)
tableView.tableHeaderView = searchController.searchBar

definesPresentationContext = true

tableView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
    tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
    tableView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
    tableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
    tableView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
])

【讨论】:

谢谢!看来问题出在不使用safeArea

以上是关于如何将 UITableView 的顶部粘贴到 UISearchBar 的底部?的主要内容,如果未能解决你的问题,请参考以下文章