问题描述
我有一个带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上
I have on my 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完成按钮始终启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!