2013-09-24 56 views
2

此代碼在iOS 6中工作得非常好,但在iOS 7中,textBar在導航欄中爲灰色,並且不可點擊?請參閱本圖片UITextfield不能作爲iOS 7的UISearchBar的子視圖嗎?

enter image description here

什麼可能是錯的區別?我不知道他們有什麼的iOS 7究竟發生變化,不知道從哪裏開始尋找解決這個問題...

/問候

UITextField *sbTextField = (UITextField *)[searchBar.subviews lastObject]; 
[sbTextField removeFromSuperview]; 

CGRect rect = searchBar.frame; 
rect.size.height = 32; 
rect.size.width = 210; 
sbTextField.frame = rect; 
// [sbTextField setKeyboardType:UIKeyboardTypeNumbersAndPunctuation]; Not working in iOS7 
// [sbTextField setPlaceholder:NSLocalizedString(@"HintSearchExercise", nil)]; Not working in iOS 7 

[sbTextField setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin]; 
[searchBar removeFromSuperview]; 

UIBarButtonItem *searchBarNavigationItem = [[UIBarButtonItem alloc] initWithCustomView:sbTextField]; 

[[self navigationItem] setLeftBarButtonItem:searchBarNavigationItem]; 
+1

爲什麼要從搜索欄中刪除文本字段?混淆標準UI控件的私有子視圖結構從來不是解決問題的好方法。 – rmaddy

+0

我刪除了代碼,我從超級視圖中蹦過搜索欄,結果仍然很亂:http://tinypic.com/view.php?pic=1056r7q&s=5#.UkIjzpKpWrg:/ – user1293618

+0

您需要顯示設置代碼顏色和外觀。您還應該刪除所有更改文本字段佈局的代碼。使用提供的「UISearchBar」API來進行所需的任何調整。不要混淆私人子視圖。 – rmaddy

回答

3

在ios7 [searchBar.subviews lastObject]文本字段而是作爲控件周圍的附加容器的一個UIView實例。具有cocktailicious同樣的問題

,我打算使用下列類別上UISearchBar

@interface UISearchBar (Workarounds) 
@property (readonly, nonatomic) UITextField *textField; 
@end 

@implementation UISearchBar (Workarounds) 
- (UITextField *)textField 
{ 
    for (UIView *view in [self subcontrols]) { 
     if ([view isKindOfClass:[UITextField class]]) { 
      return (UITextField *)view; 
     } 
    } 
    return nil; 
} 

- (NSArray *)subcontrols 
{ 
    return self.subviews.count == 1 ? [self.subviews.firstObject subviews] : self.subviews; 
} 
@end 

- subcontrols方法的伎倆在這裏。

+0

只是想補充我在Swift和iOS 8.2中遇到了同樣的問題,並且您的評論我可以修復它 – hajn

+0

同樣地!很有幫助。 – ImpurestClub

相關問題