2011-08-09 91 views

回答

11

前一個問題是這個問題,所以我認爲詢問的人已經找到了解決方案。但以防萬一其他碰巧遇到同樣的問題。這是我的解決方案。

我有一個UISearchBar帶有一個取消按鈕,只有當UISearchBar的文本框被點擊時纔會出現。因此,在UISearchBar的子類中覆蓋 - (void)layoutSubviews的解決方案對我來說不是一種選擇。無論如何,我做了一個UISearchBar(CustomSearchBar)的子類與公共方法設置取消按鈕的字體和textColor。當我創建UISearchBar時,我確保搜索欄的文本字段委託設置爲self,並且創建搜索欄的類實現UITextFieldDelegate協議。當用戶點擊搜索欄的文本字段時,會通知其委託並調用CustomSearchBar的方法。我之所以這樣做,是因爲這是取消按鈕出現的時刻,因此我知道它在視圖層次結構中,我可以進行自定義。

下面的代碼:

對於MyRootViewController在MyRootViewController創建的UISearchBar

CustomSearchBar *searchBar = [[CustomSearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 40)]; 
[searchBar setBarStyle:UIBarStyleDefault]; 
[searchBar setTintColor:[UIColor whiteColor]]; 

for (UIView *view in [searchBar subviews]) 
{ 
    if ([view isKindOfClass:[UITextField class]]) 
    { 
     UITextField *searchTextField = (UITextField *)view; 
     [searchTextField setDelegate:self]; 
    } 
} 

self.searchBar = searchBar; 
[searchBar release]; 

UITextFieldDelegate(確保它實現了UITextFieldDelegate協議)

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    [self.searchBar setCloseButtonFont:[UIFont fontWithName:@"American Typewriter" size:14] textColor:[UIColor grayColor]]; 
} 

這是公衆方法在UISearchBar的子類中

- (void)setCloseButtonFont:(UIFont *)font textColor:(UIColor *)textColor 
{ 
    UIButton *cancelButton = nil; 

    for(UIView *subView in self.subviews) 
    { 
     if([subView isKindOfClass:[UIButton class]]) 
     { 
      cancelButton = (UIButton*)subView; 
     } 
    } 

    if (cancelButton) 
    { 
     /* For some strange reason, this code changes the font but not the text color. I assume some other internal customizations  make this not possible: 

     UILabel *titleLabel = [cancelButton titleLabel]; 
     [titleLabel setFont:font]; 
     [titleLabel setTextColor:[UIColor redColor]];*/ 

     // Therefore I had to create view with a label on top:   
     UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(2, 2, kCancelButtonWidth, kCancelButtonLabelHeight)]; 
     [overlay setBackgroundColor:[UIColor whiteColor]]; 
     [overlay setUserInteractionEnabled:NO]; // This is important for the cancel button to work 
     [cancelButton addSubview:overlay]; 

     UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, kCancelButtonWidth, kCancelButtonLabelHeight)]; 
     [newLabel setFont:font]; 
     [newLabel setTextColor:textColor]; 
     // Text "Cancel" should be localized for other languages 
     [newLabel setText:@"Cancel"]; 
     [newLabel setTextAlignment:UITextAlignmentCenter]; 
     // This is important for the cancel button to work 
     [newLabel setUserInteractionEnabled:NO]; 
     [overlay addSubview:newLabel]; 
     [newLabel release]; 
     [overlay release]; 
    } 
} 
+0

這真棒。非常感謝。我用另一種方法,我不是很滿意。但有了這個,我現在可以重新訪問這個問題。謝謝! – Zhen

+0

我很高興它幫助你。 – strave

2

你也可以繼承的UISearchBar,寫自己- (void)layoutSubviews方法。在這個方法中,循環通過它的子視圖並獲取cancelButton。其餘的應該是直截了當的。

+0

感謝您的回答。不過,我不太瞭解如何重寫layoutSubView方法。你能幫我指導一下嗎? – Zhen

+0

覆蓋 - (void)layoutSubviews只有在UISearchBar的屬性顯示取消按鈕設置爲YES時纔有效。默認情況下,它被設置爲NO,因此取消按鈕僅在UISearchBar的文本字段被擊中後纔會出現。在這種情況下,覆蓋layoutSubviews將不起作用。 – strave

7

而不是做所有這些花哨的東西只是執行SearchBarTextDidBeginEditing這樣

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar 
{ 
    // only show the status bar’s cancel button while in edit mode sbar (UISearchBar) 
    searchBar.showsCancelButton = YES; 
    searchBar.autocorrectionType = UITextAutocorrectionTypeNo; 
    UIColor *desiredColor = [UIColor colorWithRed:212.0/255.0 green:237.0/255.0 blue:187.0/255.0 alpha:1.0]; 


    for (UIView *subView in searchBar.subviews){ 
     if([subView isKindOfClass:[UIButton class]]){ 
      NSLog(@"this is button type"); 

      [(UIButton *)subView setTintColor:desiredColor]; 
      [(UIButton *)subView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
     } 
} 
+0

這工作就像一個魅力! – Giovanni

4

Gyanerdra的回答運作良好。但對於iOS7,我需要進行以下更改才能在我的應用程序中工作。

NSArray *childViews; 
if ((APP).isIOS7) { 
    childViews = [[searchBar.subviews objectAtIndex:0] subviews]; 
} else { 
    childViews =searchBar.subviews; 
} 

for (UIView *subView in childViews) { 
    if([subView isKindOfClass:[UIButton class]]){ 
     [(UIButton *)subView setTintColor:desiredColor]; 
     [(UIButton *)subView setTitleColor:desiredColor forState:UIControlStateNormal]; 
    } 
} 

看來,對於iOS7搜索欄被包含在父視圖中。 希望這可以幫助別人。 b

0

KVC

的UIButton *按鈕= [_searchBar valueForKey:@ 「_ cancelButton」]; button.titleLabel.font = [UIFont systemFontOfSize:13];

+1

這看起來像是影響字體大小,而不是顏色。 – ouflak

+0

所以,你可以改變字體,你也可以改變titleColor。 [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; – fengbai

相關問題