iphone UISearchBar 完成按钮始终启用 [英] iphone UISearchBar Done button always enabled

查看:19
本文介绍了iphone UISearchBar 完成按钮始终启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 UISearchBarUIViewController.我已将搜索按钮替换为完成按钮.

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 完成按钮始终启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆