iphone UISearchBar 完成按钮始终启用

iphone UISearchBar Done button always enabled(iphone UISearchBar 完成按钮始终启用)
本文介绍了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 完成按钮始终启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

No resource found that matches the given name: attr #39;android:keyboardNavigationCluster#39;. when updating to Support Library 26.0.0(找不到与给定名称匹配的资源:attr android:keyboardNavigationCluster.更新到支持库 26.0.0 时) - IT屋-程序员软
How to resize UITextView on iOS when a keyboard appears?(出现键盘时如何在iOS上调整UITextView的大小?)
How to reliably detect if an external keyboard is connected on iOS 9?(如何可靠地检测 iOS 9 上是否连接了外部键盘?)
How do I dismiss the iOS keyboard?(如何关闭 iOS 键盘?)
Is possible to simulate touch event using an external keyboard on ios jailbroken?(是否可以在 ios 越狱后使用外部键盘模拟触摸事件?)
Numeric Soft Keyboard on Android(Android 上的数字软键盘)