简体   繁体   中英

How to stick top of the UITableView to the bottom of the UISearchBar?

I have a 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)

and my search bar is:

    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

but I wish to set my top anchor as:

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

but not as:

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

But when I stick it to the bottomAnchor of searchBar the app crashes because of the different hierarchies.

'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.'

How can I solve the problem? I'm trying to solve this problem already 3 days and no success

Option A (search bar in navigation bar):

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),
])

Option B (search bar as table header view):

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),
])

选项A. 选项B.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM