2010-07-17 48 views

回答

3

你可以改变你的UISearchBar对象的keyboardType财产。但是,没有办法直接更改returnKeyType。您可能能够过滤并手动更改它。检查UISearchBar的文档并查看是否可以找到returnKeyType,因为这正是您正在寻找的。

+0

感谢,但不幸的是在搜索栏中没有这个属性.. 。:( – SpaceDog 2010-07-17 02:21:51

2

我做到这样说:

// -- Basic UISearchBar setup. 
self.theSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,38)]; 
[self.theSearchBar setDelegate:self]; 
[self.view addSubview:self.theSearchBar]; 

// -- Customize the returnKeyType of the search bar's nested UITextField. 
UITextField *searchBarTextField = [[self.theSearchBar subviews] objectAtIndex:1]; 
searchBarTextField.returnKeyType = UIReturnKeyGo; 

希望这是有帮助的。这种方法(即通过索引获取子视图)可能会在未来中断,但现在它可以正常工作。

-1

不要依赖它作为第二个子视图,使用isKindOfClass:方法来检查。这将是更多的iOS更新证明。

for (UIView *subview in self.theSearchBar.subviews) { 
    if ([subview isKindOfClass:[UITextField class]]) { 
     [(UITextField *)subview setReturnKeyType:UIReturnKeyGo]; 
     break; 
    } 
} 
0
for (UIView *view in _searchBar.subviews){ 
      if ([view isKindOfClass:[UITextField class] ]) { 
       UITextField *searchTf = (UITextField *)view; 
       searchTf.returnKeyType = UIReturnKeyDone; 
      } 
} 
0

这是工作的iOS 6

UITextField *searchBarTextField = [[searchBarObj subviews] objectAtIndex:1]; 
    searchBarTextField.returnKeyType = UIReturnKeyDefault; 

    [searchBarTextField setEnablesReturnKeyAutomatically:NO]; 

这是工作的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; 
     } 
    } 
}