2013-09-24 71 views
3

我需要在UISearchBar中設置自定義inputView。UISearchBar的自定義InputView無法在iOS7中工作

所以我寫了下面的代碼,這是在iOS5和iOS6完美工作。

但是它在iOS7中不起作用。

for(int i =0; i<[self.searchBar.subviews count]; i++) { 
      if([[self.searchBar.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) 
      { 
       [(UITextField*)[searchBar.subviews objectAtIndex:i] setFont:[UIFont fontWithName:@"MyCustomFont" size:15]]; 
       UITextField* search=(UITextField*)[searchBar.subviews objectAtIndex:i]; 
       search.delegate = self; 

       [(UITextField*)[self.searchBar.subviews objectAtIndex:i] setInputView:self.customKeyboard]; 
       [self.customKeyboard setTextView:search]; 
      } 
     } 



     [self.sBar reloadInputViews]; 

我該怎麼做?

回答

6

搜索欄子視圖層次結構中iOS7被更改,請嘗試以下:

iOS7:

NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews]; 

iOS6的之前:

NSArray *searchBarSubViews = self.searchBar.subviews; 

嘗試下面更新功能:

NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews]; 
for(int i =0; i<[searchBarSubViews count]; i++) { 
    if([[searchBarSubViews objectAtIndex:i] isKindOfClass:[UITextField class]]) 
    { 
     UITextField* search=(UITextField*)[searchBarSubViews objectAtIndex:i]; 
     [search setFont:[UIFont fontWithName:@"MyCustomFont" size:15]]; 
     search.delegate = self; 

     [search setInputView:self.customKeyboard]; 
     [self.customKeyboard setTextView:search]; 
    } 
} 
[self.sBar reloadInputViews]; 
+0

那麼我怎麼能設置InputView與NSArray? –

+0

如果在第二行if([[[[[self.searchBar.subviews objectAtIndex:0] subviews] objectAtIndex:i] isKindOfClass:[UITextField class]])等等,則通過更改其他參數。 – null

+0

不工作的兄弟。 :( –

相關問題