2012-07-23 34 views
1

我可以在不覆蓋drawRect方法的情況下執行此操作嗎?使UIToolBar和UISearchBar具有啞光黑色背景

我已經試過如下:

UIImage *navBarImage = [[UIImage imageNamed:@"image_navbg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)]; 
[self.descriptionBar setBackgroundColor:[UIColor colorWithPatternImage:navBarImage]]; 
[self.bottomBar setBackgroundColor:[UIColor colorWithPatternImage:navBarImage]]; 

凡image_navbg.png是一個堅實的磨砂黑方。我得到的結果仍然有一個黑灰色的漸變。

而且設置這些並沒有幫助:

[self.descriptionBar setTranslucent:YES]; 
[self.bottomBar setTranslucent:YES]; 
+0

檢查此答案 - http://stackoverflow.com/questions/4904877/remove-gradient-on-uinavigationbar – kgu87 2012-07-24 01:49:25

回答

2

如果你的目標的iOS 5,用UIAppearance這個 - 不覆蓋-drawRect:了。

3

我想用內置的工具欄做同樣的事情,導航控制器。您揭示它使用:

[self.navigationController setToolbarHidden:NO]; 

爲了去除漸變中樣的工具欄(在iOS 4的和至少提前),我成功地使用了以下技術。首先,我已將此添加到我的appDelegate頭:

@interface MyToolbar : UIToolbar {} 

- (void)drawRect:(CGRect)rect; 

@end 

然後我說這對我的appDelegate .m文件

@implementation MyToolbar 

- (void)drawRect:(CGRect)rect { 
    UIColor *colorTabBlack = [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetFillColor(context, CGColorGetComponents([colorTabBlack CGColor])); 
    CGContextFillRect(context, rect); 
} 
@end 

然後在IB我切換(現在可見的)工具欄上的類「MyToolbar」 - 我永遠花了我的時間來解決這個問題,這就是我發佈它的原因。希望它可以幫助別人!

0

這是我做到的。爲我工作很好。您可能必須在子視圖中嘗試不同的索引。

// Change color of searchbar 
[[self.searchbar.subviews objectAtIndex:0] removeFromSuperview]; 
self.searchbar.backgroundColor = [UIColor blackColor]; 

// Change color of toolbar 
[[self.toolbar.subviews objectAtIndex:0] removeFromSuperview]; 
self.toolbar.backgroundColor = [UIColor blackColor];