2010-04-24 73 views

回答

96

对于命名tablesearchbar搜索栏:

// 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 
       } 
      } 
     } 
+0

它的工作原理。神奇的答案。非常感谢。 – 2010-04-25 22:52:29

+0

谢谢JK! – ambertch 2010-04-30 00:37:51

+0

这是否会超过App Store审批流程? – zekel 2010-06-17 00:26:41

0

由于警报式键盘是半透明的,我可以看到我的背后视图。它看起来不太好,因为我在键盘背后有多个元素,这使得键难以突出。我想要一个全黑键盘。

因此,当编辑文本时,我将黑色的UIImageView动画到键盘后面的位置。这给出了全黑键盘的外观。

- (void)textFieldDidBeginEditing:(UITextField *)textField { 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.25]; 

    blackBoxForKeyboard.frame = CGRectMake(0, 377, 320, 216); 
    [UIView commitAnimations]; 

} 
18

一个更有用的提示,向运行循环代码(在 “@try”)部分。

这使“完成”按钮,当文本字段为空:

UITextField *tf = (UITextField *)searchBarSubview; 
tf.enablesReturnKeyAutomatically = NO; 
+1

谢谢!这也适用于iOS7的Gregory解决方案。 – jbandi 2014-03-08 17:24:27

1

,因为它与可选的方法的协议,你应该测试方法都单独,而不是尝试醒目的。

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

43

由于iOS的7测试5,运行循环的回答并没有为我工作,但这并:

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]; 
      } 
     }  
    } 
} 
+5

为防万一还不清楚,使用if/else的原因是if为iOS 6向后兼容性,其他为iOS 7. – 2013-09-18 14:13:23

+0

奇妙的答案。这适用于iOS 7.谢谢 – Katushai 2014-08-18 20:40:22

+0

这适用于iOS8 – 2015-03-31 17:05:44

0

我想这里显示的所有解决方案,和他们都不工作了我的UISearchBar(xcode5编译iOS7)。我结束了这个递归函数,它为我工作:

- (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]; 
+0

使用Xcode 5 + iOS7,文本输入将位于子子视图中而不是子视图中。所以基本上你使用我的Xcode 4解决方案并添加了递归调用。为获得最佳性能,您可以测试iOS版本,如果是iOS7,则可以浏览'subviews [0] .subviews'而不是'subviews'。 – 2014-01-15 13:21:46

41

至少为iOS 8,简单地说:

[self.searchBar setReturnKeyType:UIReturnKeyDone]; 
0

只是为了涵盖所有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; 
    } 
} 
9

对于斯威夫特改变的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 
} 
1

只是一个提醒!如果您将searchBar保留为第一响应者,那么在更改returnKeyType后,您需要关闭键盘并再次弹出以查看更改。

search.resignFirstResponder() 
searchBar.returnKeyType = UIReturnKeyType.Done 
search.becomeFirstResponder()