我创建了一个自定义的 tableView 标题,在其中我有一个 stackView 中的占位符视图,我想用 searchBar 替换它。这似乎有效,但是当我点击搜索栏时它消失了。我不确定问题是什么,我注意到当我拉动以刷新搜索栏时,它会显示并且处于活动状态。
自定义标题:
class FilterHeader: UIView {
var searchPlaceHolderView: UIView = {
let view = UIView(frame: .zero)
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .lightGray
return view
}()
let logButton: UIButton = {
let button = UIButton(type: .system)
button.backgroundColor = .white
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("Filter", for: .normal)
button.titleLabel?.font = .systemFont(ofSize: 14, weight: .regular)
button.setTitleColor(Colors.darkTextColor, for: .normal)
let backgroundImage = UIImage(named: "icon_log",
in: Bundle.frameworkBundle,
compatibleWith: nil)
button.setImage(backgroundImage, for: .normal)
button.tintColor = .gray
button.layer.cornerRadius = 6
button.heightAnchor.constraint(equalToConstant: 40).isActive = true
button.widthAnchor.constraint(equalToConstant: 80).isActive = true
button.addTarget(self, action: #selector(didTapFilter(_:)), for: .touchUpInside)
button.layer.borderColor = UIColor.lightGray.cgColor
button.layer.borderWidth = 0.3
button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 16)
button.semanticContentAttribute = .forceRightToLeft
return button
}()
let stackView: UIStackView = {
let view = UIStackView()
view.alignment = .center
view.axis = .horizontal
view.distribution = .fill
view.spacing = 8
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
init() {
super.init(frame: .zero)
setupView()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupView() {
backgroundColor = .red
stackView.addArrangedSubview(searchPlaceHolderView)
stackView.addArrangedSubview(logButton)
addSubview(stackView)
NSLayoutConstraint.activate([
self.heightAnchor.constraint(equalToConstant: 100),
stackView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16),
stackView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16),
stackView.centerYAnchor.constraint(equalTo: centerYAnchor, constant: -16)
])
}
}
在 ViewController 上,我添加了这样的自定义标头:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
let header = FilterHeader()
let searchBar = searchController.searchBar
searchBar.backgroundColor = .lightGray
header.searchView.pinToSuperViewEdges(searchBar)
return header
}
return nil
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return 100
} else {
return UITableView.automaticDimension
}
}
辅助函数
extension UIView {
func pinToSuperViewEdges(_ view: UIView, insets: UIEdgeInsets = .zero) {
view.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(view)
NSLayoutConstraint.activate([
self.leftAnchor.constraint(equalTo: view.leftAnchor, constant: insets.left),
self.topAnchor.constraint(equalTo: view.topAnchor, constant: insets.top),
self.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -insets.right),
self.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -insets.bottom)
])
}
}
我不确定我缺少什么以及导致搜索栏消失的原因。