簡體   English   中英

如何改變UINavigationBar的顏色?

[英]How to change the color of UINavigationBar?

我想改變UINavigationBar的顏色,顏色將從圖像中獲取。

嘗試將對象設置為navigationBar的子視圖。

設置色調顏色屬性或使用圖像

UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:rootController]; 
controller.navigationBar.tintColor = [UIColor blackColor];

在iOS 7或更高版本中,請嘗試以下操作:

navigationBar.barTintColor = [UIColor redColor];

如果您僅支持iOS5及更高版本,則可以使用“外觀代理”。 一個很好的教程

如果必須支持以前的版本,則必須UINavigationBar並覆蓋其drawRect方法。 一個很好的示例代碼

有一種方法可以從xcode 6.4中的storyboard中做到這一點。

從文檔大綱中選擇導航欄

比從屬性檢查器更改條紋色調

你也可以選擇其他,這將帶來顏色選擇器

選擇滴管,這將讓您從圖像中選擇一種顏色

嘗試這個:

  1. 首先創建Objective C類,說CustomNavBar繼承了UINavigationBar類...
  2. 然后在你的appDelegate中導入這個類......
  3. 然后將此代碼放在appDelegate類末尾的代碼下面......
  4. 然后打開MainWindow.xib並將UINavigationBar class to更改UINavigationBar class to CustomNavBar`。

編碼:

@implementation CustomNavBar (CustomNavBarCategory)

-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 
{

    if([self isMemberOfClass:[CustomNavBar class]])
    {
        UIImage *image;

        image=[UIImage imageNamed:@"ImageName"];

        CGContextClip(ctx);
        CGContextTranslateCTM(ctx, 0, image.size.height);
        CGContextScaleCTM(ctx, 1.0, -1.0);
        CGContextDrawImage(ctx,CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage); 

    }
    else 
    {        
        [super.layer drawLayer:layer inContext:ctx];     
    }

}  

@end

暫無
暫無

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

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