ios - 如何将 UITableView 的顶部粘贴到 UISearchBar 的底部?
问题描述
我有一个 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 天但没有成功
解决方案
选项 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),
])
推荐阅读
- subprocess - 如果我在事件循环 asyncio 中启动一组要执行的任务,我可以分别对每个任务的返回做出反应吗?
- python - 无法使用通过 sympy.diff 找到的导数函数
- c# - .NET 应用程序来获取 iis 标头和重定向
- html - Watson Assistant:Chrome 中的消息气泡间距(下边距)不起作用
- mobx - mobx 挑战:将 getter 和 setter 放入可观察数组
- reactjs - 在父函数中的 setSomeState() 函数之后执行某些函数时,React 钩子会发生什么
- javascript - 如何使用 Cypress.io 运行单元测试?
- javascript - 如何知道元素相对于另一个元素的位置
- python - 通过 GitHub 操作和无服务器框架部署 AWS Lambda 时找不到好的绑定路径格式
- php - 如何从下拉框和多个复选框中获取信息以显示我的输出