2013-11-29 75 views
1

在iOS6的,我用下面的代碼改變的UISearchBar如何在iOS7中更改UISearchBar的inputView?

的inputView
for (UIView *view in _searchBar.subviews) { 
    if ([view isKindOfClass: [UITextField class]]) { 
     UITextField* textField = (UITextField*)view; 
     [textField setInputView:_myKeyboard]; 
     break; 
    } 
} 

的UISearchBar在iOS7發生了變化,我不知道如何找到文本框來改變inputView。 請幫忙!謝謝!

回答

2

子視圖的層次結構已經改變iOS7,這樣你就可以使用下面的代碼:

// subviews 
NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews]; 
for (UIView *view in searchBarSubViews) { 
    if([view isKindOfClass:[UITextField class]]) 
    { 
     UITextField* search=(UITextField*)view; 
     [search setFont:[UIFont fontWithName:@"MyCustomFont" size:15]]; 
     search.delegate = self; 

     [search setInputView:self.customKeyboard]; 
     [self.customKeyboard setTextView:search]; 
    } 
} 
[self.searchBar reloadInputViews]; 
+1

您也可以使用以下命令從UISearchBar獲取UITextField:[self.searchBar valueForKey:@「_ searchField」] – matzino

0

試試這個代碼:

for (UIView *view in _searchBar.inputView.subviews) { 
     if ([view isKindOfClass: [UITextField class]]) { 
      UITextField* textField = (UITextField*)view; 
      [textField setInputView:_myKeyboard]; 
      break; 
     } 
    } 
在iOS的7子視圖

不工作。從iOS7 UI對象有一個額外的包裝器/容器。可能是這段代碼會幫助你。

+0

_searchBar.inputView.subviews無法正常工作。不管怎麼說,還是要謝謝你! –

1

使用下面的代碼:

NSArray *subViewsOfSearchBar = [[self.YOurSearchBar.subviews objectAtIndex:0] subviews]; 
for(int i =0; i< subViewsOfSearchBar.count; i++) { 
    if([[subViewsOfSearchBar objectAtIndex:i] isKindOfClass:[UITextField class]]) 
    { 
     UITextField *searchTxtField=(UITextField*)[subViewsOfSearchBar objectAtIndex:i]; 
     [searchTxtField setInputView:self.customKeyboard]; 
    } 
} 
[self.searchBar reloadInputViews]; 
相關問題