UISearchBar 取消按钮 [英] UISearchBar Cancel Button
本文介绍了UISearchBar 取消按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 iOS 5 上使用带有 showCancelButton=YES 的 UISearchBar.希望在键盘下降时取消按钮保持启用状态.使用以下代码似乎不起作用:
Using a UISearchBar with showCancelButton=YES on iOS 5. Would like the cancel button to stay enabled when the keyboard drops down. Using the following code seems not to work:
for (id subView in self.searchControl.subviews)
{
if ([subView isKindOfClass:[UIButton class]])
{
UIButton *cancelButton = (UIButton *)subView;
[cancelButton setEnabled:YES];
break;
}
}
subView 实际上是一个 UINavigationButton,它似乎不是 UIButton 的子类.我在这里错过了什么???????在 Apple 文档中也找不到有关 UINavigationButton 类的任何信息.
The subView is actually a UINavigationButton which appears not to be subclassed off of UIButton. What am I missing here??????? Also cannot find any info on the UINavigationButton class in the Apple docs.
推荐答案
设置您的搜索栏委托,然后放置此代码.
Set your searchbar delegate and than put this code.
- (void) searchBarSearchButtonClicked:(UISearchBar*) theSearchBar
{
[theSearchBar resignFirstResponder];
[theSearchBar setShowsCancelButton:NO animated:YES];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:YES animated:YES];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
[searchBar setShowsCancelButton:NO animated:YES];
}
斯威夫特 3.0
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
searchBar.setShowsCancelButton(false, animated: true)
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchBar.setShowsCancelButton(true, animated: true)
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
searchBar.setShowsCancelButton(false, animated: true)
}
这篇关于UISearchBar 取消按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文