自定义一个searchBar 在layoutSubview中设置搜索按钮长亮
class EliveSearchBar: UISearchBar {
override init(frame: CGRect) { super.init(frame: frame) } override func layoutSubviews() { super.layoutSubviews() if let searchTF: UITextField = self.value(forKey: "_searchField") as? UITextField{ searchTF.enablesReturnKeyAutomatically = false } } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) //fatalError("init(coder:) has not been implemented") } }
注意:获取UISearchBar中textfield
let searchTF = searchBar.value(forKey: "_searchField") as? UITextField
设置UISearchBar中的取消按钮中文显示
searchBar.setValue("取消", forKey: "_cancelButtonText")