[英]UISearchBar Keyboard Return Key
I am using a UISearchBar to match text input against entries in a database and display the matched results to the user in a UITableView, as they type. 我正在使用UISearchBar将文本输入与数据库中的条目进行匹配,并在UITableView中键入匹配的结果给用户。
All is well, however, I cannot find a way to alter the return key type of the search bar's keyboard. 一切都很好,但是,我找不到改变搜索栏键盘的返回键类型的方法。 By default it replaces the standard return key with a Search button.
默认情况下,它会使用“ 搜索”按钮替换标准返回键。 Because I am doing a live search as the user types, I do not need this button and having it there and inactive has raised some usability issues.
因为我正在进行实时搜索,因为用户输入,我不需要这个按钮并将其保持在那里并且不活动已经引发了一些可用性问题。
Attempted solutions 试图解决方案
I can set a keyboard with the setKeyboard:UIKeyboardType
method, however this doesn't seem to override the default setting of replacing the return key (on the standard keyboard) with a Search key and it does not allow access to change this return key. 我可以使用
setKeyboard:UIKeyboardType
方法设置键盘,但这似乎不会覆盖使用搜索键替换返回键(在标准键盘上)的默认设置,并且它不允许访问更改此返回键。
I have thought about using a UITextField
, giving me access to the returnKeyType
property through the UITextInputTraits
protocol. 我考虑过使用
UITextField
,让我通过UITextInputTraits
协议访问returnKeyType
属性。 My problem with this however is that I am implementing the UISearchBarDelegate
method searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
, which I would lose with the UITextField
. 我的问题是,我正在实现
UISearchBarDelegate
方法searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
,我将丢失UITextField
。
Is there a way that I can keep the functionality of the search bar's delegate methods, whilst having legitimate access to the keyboard's return key? 有没有办法可以保持搜索栏的委托方法的功能,同时合法访问键盘的返回键?
In fact, almost the exact screen I am implementing is found in Apple's Clock application 事实上,我正在实现的几乎所有屏幕都可以在Apple的Clock应用程序中找到
Screenshot: 截图:
So any help on a clean solution would be much appreciated. 因此,非常感谢任何有关清洁解决方案的帮助。 Note the return key on the bottom right instead of the default Search button'.
请注意右下角的返回键,而不是默认的“ 搜索”按钮。
Slightly different in iOS 7 compared to the answer of @sudip. 与@sudip的答案相比,iOS 7略有不同。
for (UIView *subview in self.searchBar.subviews)
{
for (UIView *subSubview in subview.subviews)
{
if ([subSubview conformsToProtocol:@protocol(UITextInputTraits)])
{
UITextField *textField = (UITextField *)subSubview;
[textField setKeyboardAppearance: UIKeyboardAppearanceAlert];
textField.returnKeyType = UIReturnKeyDone;
break;
}
}
}
I tried all of these solutions without luck until I realized that in IOS8, you can just set searchBar.returnKey = .Done
or whatever UIReturnKeyType
you like. 我没有运气就尝试了所有这些解决方案,直到我意识到在IOS8中,你可以设置
searchBar.returnKey = .Done
或你喜欢的任何UIReturnKeyType
。 Sigh. 叹。
Try this: 试试这个:
for(UIView *subView in searchBar.subviews) { if([subView conformsToProtocol:@protocol(UITextInputTraits)]) { [(UITextField *)subView setKeyboardAppearance: UIKeyboardAppearanceAlert]; } }
If you want to dismiss the return key (ie, make it do nothing), set the "returnKeyType" property on the UITextField subview to "UIReturnKeyDone" along with "keyboardAppearence". 如果要关闭返回键(即,使其不执行任何操作),请将UITextField子视图上的“returnKeyType”属性设置为“UIReturnKeyDone”以及“keyboardAppearence”。
I had to add some lines to Neo's answer. 我不得不为Neo的回答添加一些内容。 Here is my code to add a "Done" button for UISearchbar :
这是我为UISearchbar添加“完成”按钮的代码:
for(UIView *subView in sb_manSearch.subviews) {
if([subView conformsToProtocol:@protocol(UITextInputTraits)]) {
UITextField *t = (UITextField *)subView;
[t setKeyboardAppearance: UIKeyboardAppearanceAlert];
t.returnKeyType = UIReturnKeyDone;
t.delegate = self;
break;
}
}
To change Search into Done text. 将搜索更改为完成文本。 Use below code.
使用以下代码。
youtSearchBar.returnKeyType = .done
UPDATE : From iOS 7 onwards, the accepted answer will not work , below version will the work on iOS 7 onwards. 更新:从iOS 7开始, 接受的答案将无效 ,低于版本将在iOS 7以上开始工作。
UIView *subViews = [[_searchBar subviews] firstObject];
for(UIView *subView in [subViews subviews]) {
if([subView conformsToProtocol:@protocol(UITextInputTraits)]) {
[(UITextField *)subView setEnablesReturnKeyAutomatically:NO];
}
}
for (UIView *subView in view.subviews) {
if ([subView isKindOfClass:[UITextField class]])
{
UITextField *txt = (UITextField *)subView;
@try {
[txt setReturnKeyType:UIReturnKeyDone];
[txt setKeyboardAppearance:UIKeyboardAppearanceAlert];
}
@catch (NSException * e) {
// ignore exception
}
}
}
you can do it by : 你可以通过以下方式完成:
- (void)viewDidLoad
{
// Adding observer that will tell you keyboard is appeared.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification object:nil];
[super viewDidLoad];
}
- (void)keyboardDidShow:(NSNotification *)note
{
keyboardTest = [self getKeyboard];
[keyboardTest setReturnKeyEnabled: YES];
}
- (id) getKeyboard // Method that returns appeared keyboard's reference
{
id keyboardView;
// locate keyboard view
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++)
{
keyboard = [tempWindow.subviews objectAtIndex:i];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2)
{
if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
{
keyboard = [[keyboard subviews] objectAtIndex:0];
keyboardView = keyboard ;
}
}
else
{
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
keyboardView = keyboard ;
}
}
return keyboardView ;
}
I just found the simplest wait to hack this, just put a blank when beginning editing search field 我刚刚发现了最简单的等待,只需在开始编辑搜索字段时留空
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
//Add a blank character to hack search button enable
searchBar.text = @" ";}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.