我的代码中包含以下几行:
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchBar.showsCancelButton = true
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
当我点击searchButton时,键盘被关闭,取消按钮消失了。
解决办法是什么 ?
最佳答案
解决了!
我已经通过在UISearchBar旁边添加UIButton [cancelButton]解决了该问题,并将宽度约束添加为@IBOutlet。现在,我没有显示searchBar的cancelButton,而是更改了cancelButton的宽度以显示或隐藏,如下所示:
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
UIView.animateWithDuration(0.25) {
self.widthConstraint.constant = 44.0
self.cancelButton.layoutIfNeeded()
}
}
@IBAction func cancelButtonTapped(sender: UIButton) {
UIView.animateWithDuration(0.25) {
self.widthConstraint.constant = 0.0
self.cancelButton.layoutIfNeeded()
}
}