2010-07-17 49 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; 
     } 
    } 
}