ios - 无法在 HeaderView(tableview 的标题视图)中添加 UISearchBar?
问题描述
无法在 tableView 部分标题中看到 searchBar 一切都是以编程方式完成的,没有使用情节提要
import UIKit
class ConversationsViewController: UITableViewController {
let searchBar:UISearchBar = {
let bar = UISearchBar()
bar.placeholder = "search conversations"
bar.translatesAutoresizingMaskIntoConstraints = false
return bar
}()
let headerView:UIView = {
let hView = UIView()
// hView.backgroundColor = .red
hView.translatesAutoresizingMaskIntoConstraints = false
return hView
}()
// we want custom cell and header view
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
}
// override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
// return "section \(section)"
// }
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
headerView.addSubview(searchBar)
searchBar.centerXAnchor.constraint(equalTo: headerView.centerXAnchor).isActive = true
searchBar.centerYAnchor.constraint(equalTo: headerView.centerYAnchor).isActive = true
searchBar.heightAnchor.constraint(equalToConstant: 100).isActive = true
searchBar.widthAnchor.constraint(equalToConstant: 40).isActive = true
return headerView
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 70
}
}
我尝试了不同的方法,但我发现这种方法可以在 tableView 中添加 headerView,现在我无法弄清楚如何在 headerView 中添加该搜索栏,我在上面尝试了但它不起作用,请帮助谢谢
解决方案
几件事...
节标题的视图不应设置.translatesAutoresizingMaskIntoConstraints
为false
. 将其保留为默认值true
。
不要给出UISearchBar
高度约束——让它使用它的固有高度。
我们确实需要给搜索栏一个宽度限制。如果您希望它适合表格的宽度,请使用:
searchBar.widthAnchor.constraint(equalTo: headerView.widthAnchor, constant: 0.0).isActive = true
如果你希望它只是部分宽度,要么给那条线一个负值constant
,要么使用一个乘数:
// this will make the search bar 90% of the width of the table
searchBar.widthAnchor.constraint(equalTo: headerView.widthAnchor, multiplier: 0.9).isActive = true
这是您的课程进行了这些修改:
class ConversationsViewController: UITableViewController {
let searchBar:UISearchBar = {
let bar = UISearchBar()
bar.placeholder = "search conversations"
bar.translatesAutoresizingMaskIntoConstraints = false
return bar
}()
let headerView:UIView = {
let hView = UIView()
// give it a background color so we can easily see
// if it is laying out correctly
hView.backgroundColor = .red
// section header view should leave this at the default TRUE
//hView.translatesAutoresizingMaskIntoConstraints = false
return hView
}()
override func viewDidLoad() {
super.viewDidLoad()
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
// add the searchBar
headerView.addSubview(searchBar)
// center it horizontally and vertically
searchBar.centerXAnchor.constraint(equalTo: headerView.centerXAnchor).isActive = true
searchBar.centerYAnchor.constraint(equalTo: headerView.centerYAnchor).isActive = true
// don't set height constraint - use UISearchBar intrinsic height
//searchBar.heightAnchor.constraint(equalToConstant: 100).isActive = true
// constrain width equal to headerView width
// we can adjust the constant if we don't want it to span the entire width of the table
// by using a negative value for the constant, or using a mulitplier to get a percent of the width
searchBar.widthAnchor.constraint(equalTo: headerView.widthAnchor, constant: 0.0).isActive = true
return headerView
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 70
}
}
推荐阅读
- python - 我如何找出特定城市的县人口普查区
- r - 如何使 ar 绘图上的 y 轴以带逗号的十进制格式显示数字?
- java - 在没有模型的情况下使用 th:object
- firebase - 如何使用 Firebase 内置功能而不是 ORM?
- database - Asp.net 核心数据库数据显示的频率高于现有数据
- c# - 在 WPF 项目中使用 NavigationView
- azure-devops-server - Azure Devops 克隆到新环境
- rust - BigUint::from(24u32) 做什么?
- php - 在 Laravel 8 中,如何在单元测试时获取请求的 url?
- python - 如何将以下代码的数据写入 json 文件,我收到以下错误,任何人都可以帮助我的代码有什么问题