iphone UISearchBar Done button always enabled(iphone UISearchBar 完成按钮始终启用)
问题描述
我有一个带有 UISearchBar
的 UIViewController
.我已将搜索按钮替换为完成按钮.
I have a UIViewController
with a UISearchBar
. I have replaced the Search Button by a Done button.
但是,当点击搜索栏时,完成按钮最初是禁用
.在输入任何字符之前都会发生这种情况.
However, when one taps on the searchbar, the Done button is initially disabled
. This occurs until one enters any character.
我想要做的是让这个完成"按钮始终启用
,这样如果我点击它,我可以立即关闭键盘.
What I want to do is to have this Done button always enabled
, such that if i tap on it i can inmediately dismiss the keyboard.
有什么帮助吗?将不胜感激.
Any help? it would be highly appreciated.
我的 UIViewController
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
return YES;
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchBar.text.length == 0)
{
//[self fixOrientation];
[searchBar resignFirstResponder];
}
else
{
NSLog(@"typed");
}
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar
{
NSLog(@"began"); // this executes as soon as i tap on the searchbar, so I'm guessing this is the place to put whatever solution is available
}
推荐答案
现在的 UISearchBar
符合 UITextInputTraits
.您可以简单地设置:
Nowadays UISearchBar
conforms to UITextInputTraits
. You can simply set:
searchBar.enablesReturnKeyAutomatically = NO;
警告:虽然这是为 iOS 7.0 编译的,但它会在运行时崩溃.它仅适用于 >=7.1.
WARNING: While this compiles for iOS 7.0, it will crash at runtime. It only works for >=7.1.
文档并不清楚这一点,因为仅从 7.1 开始,UISearchBar
实现了 UITextInputTraits
协议,但没有说明该协议是哪个 iOS 版本采纳.
The docs are not clear on this one, as only since 7.1, the UISearchBar
implements the UITextInputTraits
protocol, but it is not noted since which iOS version the protocol is adopted.
这篇关于iphone UISearchBar 完成按钮始终启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iphone UISearchBar 完成按钮始终启用
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01