我需要向 searchTextField.RightView 添加一个View ,但是当我激活RightViewMode时,会显示一个不是我创建的 ImageView (放大镜)。
我的自定义搜索条码:
override init(frame: CGRect) {
super.init(frame: .zero)
setupLayout()
}
override func setNeedsLayout() {
super.setNeedsLayout()
self.searchTextField.rightViewMode = .always
}
private func setupLayout() {
self.searchBarStyle = UISearchBar.Style.default
self.placeholder = "Search..."
self.sizeToFit()
self.isTranslucent = false
self.returnKeyType = .search
self.searchTextField.leftView?.tintColor = .darkText
self.showsCancelButton = true
let view = UIView()
view.backgroundColor = .red
view.snp.makeConstraints({ make in
make.size.equalTo(26)
})
self.searchTextField.rightView = view
}
视图控制器
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(customSearchBar)
customSearchBar.snp.makeConstraints { make in
make.leading.trailing.equalToSuperview()
make.height.equalTo(30)
make.center.equalToSuperview()
}
}