2013-10-03 39 views
1

我有以下代碼可以更改UISearchBar中「取消」按鈕的背景。更改取消按鈕BackgroundColor和UISearchBar在iOS7中的文本

for(UIView *subview in self.searchBar.subviews){ 
    if ([subview isKindOfClass:[UIButton class]]) { 
     [(UIButton *)subview setEnabled:YES]; 
     [(UIButton *)subview setTitle:@"New Button Text" forState:UIControlStateNormal]; 
     ((UIButton *)subview).tintColor = [UIColor colorWithRed:4/255.0 green:119/255.0 blue:152/255.0 alpha:1.0]; 
    } 
} 

問題是這段代碼在iOS7中不起作用! UISearchBar中的視圖結構發生了什麼變化?

編輯:這裏測試,這是的UISearchBar的新層次:

UISearchBar: 
---UIView: 
------UISearchBarBackground 
------UISearchBarTextField 
------UINavigationButton 

的問題是,我無法測試if ([sub isKindOfClass:[UINavigationButton class]])。這條線拋出一個編譯錯誤:Use of undeclared identifier: UINavigationButton

回答

1

解決方案:我創造了我自己的按鈕。這樣我就可以管理所有屬性,而無需瞭解Apple對元素所做的操作

我只需創建UIView aux即可包含searchBar和新Button。

self.searchBar.showsCancelButton = NO; //don`t show the original cancelButton 

self.cancelSearchButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
self.cancelSearchButton.frame = CGRectMake(250, 6, 60, 31); 
[self.cancelSearchButton setTitle:@"Cancelar" forState:UIControlStateNormal]; 
[self.cancelSearchButton addTarget:self action:@selector(searchBarCancelButtonClicked) forControlEvents:UIControlEventTouchUpInside]; 
NSString *fontName = [[self.cancelSearchButton titleLabel] font].fontName; 
[[self.cancelSearchButton titleLabel] setFont:[UIFont fontWithName:fontName size:11.0]]; 
UIView *aux = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
aux.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0]; 
aux.layer.borderWidth = 1.0f; 
aux.layer.borderColor = [UIColor lightGrayColor].CGColor; 
aux.layer.cornerRadius = 0.0f; 
[aux addSubview:self.searchBar]; 
[aux addSubview:self.cancelSearchButton]; 

- (void)searchBarCancelButtonClicked{ 
    //do whatever I want to do here 
} 
+0

這個方法的問題是默認情況下你不會得到漂亮的默認動畫...或者如果你能我想知道如何。 – x89a10

6

[Edited]

試試這個代碼。

加入AppDelegate如果你想改變所有取消按鈕。

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
                             [UIColor redColor], 
                             UITextAttributeTextColor, 
                             [UIColor whiteColor], 
                             UITextAttributeTextShadowColor, 
                             [NSValue valueWithUIOffset:UIOffsetMake(0, 0)], 
                             UITextAttributeTextShadowOffset, 
                             nil] 
                          forState:UIControlStateNormal]; 

在iOS7,我們需要添加objectAtIndex:0 and subviews.

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

in iOS7: 

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


iOS6 and before: 

NSArray *searchBarSubViews = self.searchBar.subviews; 
+1

很抱歉,但它沒有工作。 –

+0

我編輯了我的答案,請重新檢查它。它在我的項目中工作正常。 –

+0

這也不適合我。 @Yahiko,這是從iOS 7.0.3仍然工作?迭代子視圖時,我只看到2個元素;沒有UIBarButtonItems或UIButtons。困惑。 – Daniel

0

您可以利用iOS運行時屬性_cancelButton來實現此目的。

UIButton *cancelButton = [self.searchBar valueForKey:@"_cancelButton"]; 
[cancelButton setTitleColor:[UIColor yourColor] forState:UIControlStateNormal]; 

對於更改文本

[self.searchBar setValue:@"customString" forKey:@"_cancelButtonText"]; 
相關問題