我有一个带有 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 完成按钮始终启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
出现键盘时如何在iOS上调整UITextView的大小?How to resize UITextView on iOS when a keyboard appears?(出现键盘时如何在iOS上调整UITextView的大小?)
如何可靠地检测 iOS 9 上是否连接了外部键盘?How to reliably detect if an external keyboard is connected on iOS 9?(如何可靠地检测 iOS 9 上是否连接了外部键盘?)
如何关闭 iOS 键盘?How do I dismiss the iOS keyboard?(如何关闭 iOS 键盘?)
是否可以在 ios 越狱后使用外部键盘模拟触摸事件Is possible to simulate touch event using an external keyboard on ios jailbroken?(是否可以在 ios 越狱后使用外部键盘模拟触摸事件?)
Android 上的数字软键盘Numeric Soft Keyboard on Android(Android 上的数字软键盘)
如何从 iOS 应用程序上的自定义键盘检索击键?How do I retrieve keystrokes from a custom keyboard on an iOS app?(如何从 iOS 应用程序上的自定义键盘检索击键?)