我的代码中有以下几行:
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchBar.showsCancelButton = true
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
当我点击搜索按钮时,键盘消失,取消按钮消失。
解决方案是什么?
最佳答案
已解决!
我通过在 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()
}
}
关于ios - 解除 UISearchBar 键盘后取消按钮消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38193471/