本文介绍了如何将UITableView的顶部粘贴到UISearchBar的底部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个UTableView:
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)
而我的搜索栏是:
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)
但不是:
tableView.topAnchor.constraint(equalTo: view.topAnchor)
但是当我将其粘贴在searchBar的bottomAnchor上时
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.'
我该如何解决这个问题?
我已经尝试解决此问题3天,没有成功
How can I solve the problem?I'm trying to solve this problem already 3 days and no success
推荐答案
选项A(导航栏中的搜索栏bar):
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),
])
选项B(搜索栏为表标题视图)
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),
])
这篇关于如何将UITableView的顶部粘贴到UISearchBar的底部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!