更改UISearchBar /键盘searchbutton标题
在UISearchBar控件中,是否将键盘的search键标题更改为完成?
对于名为tablesearchbar的search栏:
// Set the return key and keyboard appearance of the search bar for (UIView *searchBarSubview in [tableSearchBar subviews]) { if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)]) { @try { [(UITextField *)searchBarSubview setReturnKeyType:UIReturnKeyDone]; [(UITextField *)searchBarSubview setKeyboardAppearance:UIKeyboardAppearanceAlert]; } @catch (NSException * e) { // ignore exception } } }
从iOS 7 beta 5开始,Run Loop的答案对我来说不起作用,但是这样做:
for(UIView *subView in [searchBar subviews]) { if([subView conformsToProtocol:@protocol(UITextInputTraits)]) { [(UITextField *)subView setReturnKeyType: UIReturnKeyDone]; } else { for(UIView *subSubView in [subView subviews]) { if([subSubView conformsToProtocol:@protocol(UITextInputTraits)]) { [(UITextField *)subSubView setReturnKeyType: UIReturnKeyDone]; } } } }
至less在iOS 8中,简单地说:
[self.searchBar setReturnKeyType:UIReturnKeyDone];
一个更有用的提示, 运行循环代码(在“@try”)部分。
当文本字段为空时,这启用了“完成”button:
UITextField *tf = (UITextField *)searchBarSubview; tf.enablesReturnKeyAutomatically = NO;
Swift更改UISearchBar的返回键
searchBar.returnKeyType = UIReturnKeyType.Done
枚举可用如下
public enum UIReturnKeyType : Int { case Default case Go case Google case Join case Next case Route case Search case Send case Yahoo case Done case EmergencyCall @available(iOS 9.0, *) case Continue }
由于它是一个包含可选方法的协议,因此您应该单独testing每个方法而不是尝试捕获。
for (UIView *searchBarSubview in searchBar.subviews) { if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)]) { // keyboard appearance if ([searchBarSubview respondsToSelector:@selector(setKeyboardAppearance:)]) [(id<UITextInputTraits>)searchBarSubview setKeyboardAppearance:UIKeyboardAppearanceAlert]; // return key if ([searchBarSubview respondsToSelector:@selector(setReturnKeyType:)]) [(id<UITextInputTraits>)searchBarSubview setReturnKeyType:UIReturnKeyDone]; // return key disabled when empty text if ([searchBarSubview respondsToSelector:@selector(setEnablesReturnKeyAutomatically:)]) [(id<UITextInputTraits>)searchBarSubview setEnablesReturnKeyAutomatically:NO]; // breaking the loop when we are done break; } }
这将适用于iOS <= 6.对于iOS> = 7,您需要在searchBar.subviews[0].subviews
。
由于警报式键盘是半透明的,所以我可以看到我的观点。 它看起来不太好,因为我有多个键盘背后的元素,使键很难突出。 我想要一个全黑键盘。
所以当我编辑文本时,我将黑色的UIImageView设置到了键盘后面的位置。 这给出了全黑键盘的外观。
- (void)textFieldDidBeginEditing:(UITextField *)textField { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.25]; blackBoxForKeyboard.frame = CGRectMake(0, 377, 320, 216); [UIView commitAnimations]; }
我尝试了这里显示的所有解决scheme,没有一个为我的UISearchBar(xcode5编译为iOS7)工作。 我结束了这个为我工作的recursion函数:
- (void)fixSearchBarKeyboard:(UIView*)searchBarOrSubView { if([searchBarOrSubView conformsToProtocol:@protocol(UITextInputTraits)]) { if ([searchBarOrSubView respondsToSelector:@selector(setKeyboardAppearance:)]) [(id<UITextInputTraits>)searchBarOrSubView setKeyboardAppearance:UIKeyboardAppearanceAlert]; if ([searchBarOrSubView respondsToSelector:@selector(setReturnKeyType:)]) [(id<UITextInputTraits>)searchBarOrSubView setReturnKeyType:UIReturnKeyDone]; if ([searchBarOrSubView respondsToSelector:@selector(setEnablesReturnKeyAutomatically:)]) [(id<UITextInputTraits>)searchBarOrSubView setEnablesReturnKeyAutomatically:NO]; } for(UIView *subView in [searchBarOrSubView subviews]) { [self fixSearchBarKeyboard:subView]; } }
然后我这样称呼它:
_searchBar = [[UISearchBar alloc] init]; [self fixSearchBarKeyboard:_searchBar];
只是为了涵盖所有的iOS版本:
NSArray *subviews = [[[UIDevice currentDevice] systemVersion] floatValue] < 7 ? _searchBar.subviews : _searchBar.subviews[0].subviews; for (UIView *subview in subviews) { if ([subview conformsToProtocol:@protocol(UITextInputTraits)]) { UITextField *textField = (UITextField *)subview; [textField setKeyboardAppearance: UIKeyboardAppearanceAlert]; textField.returnKeyType = UIReturnKeyDone; break; } }
只是提醒! 如果searchBar保持为第一响应者,那么在更改returnKeyType之后,需要closures键盘并再次popup以查看更改。
search.resignFirstResponder() searchBar.returnKeyType = UIReturnKeyType.Done search.becomeFirstResponder()