簡體   English   中英

將NavigationBar背景設置為純色

[英]Set NavigationBar background to a solid color

有什么辦法可以將UINavigationController導航欄的背景設置為純色嗎?

我知道我可以改變Tint顏色,但這仍然留給我漸變/玻璃效果。

我可以用任何方式擺脫它,只是有一個簡單的舊純色?

以下代碼還會導致導航欄的純色:

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundColor:[UIColor redColor]];

我認為你必須-(void)drawRect:(CGRect)rect UINavigationBar並覆蓋-(void)drawRect:(CGRect)rect

UIColor *colorFlat = /* any UIColor*/
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [colorFlat CGColor]);
CGContextFillRect(context, rect);

我曾經覆蓋drawRect方法並填充顏色; 但是在iOS7升級之后,它會導致UINavigationBar出現一些問題。 如果您編寫自己的drawrect方法,即使您調用[super drawRect],它也會更改條​​形的尺寸,最終會得到一個高度為44像素的navigationBar。 狀態欄保留為空。

為了得到一個純色的navigationBar,我使用了一個圖像作為背景圖像(任何小的純色圖像都可以自己拉伸它)並在UINavigationBar子類的initWithFrame方法中添加這些行:

[self setBackgroundColor:[UIColor clearColor]]     
[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"bgimage.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5) resizingMode:UIImageResizingModeStretch] forBarMetrics:UIBarMetricsDefault]; 

以下代碼對我有用:

[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]];
self.navigationController.navigationBar.translucent = NO;
[self.navigationController.navigationBar setShadowImage:[UIImage new]];

創建一個CustomUIViewController ,擴展UIViewController並覆蓋viewDidLoad ,如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 1.0 alpha: 1.0];
}

之后,在控制器中使用CustomUIViewController。

積分

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM