设置导航栏控制器背景色

设置导航栏控制器背景色

 

效果

 

说明

1. 去掉了原始导航栏控制器上面的黑线

2. 可以定制背景透明度

 

源码

复制代码
//
//  AppDelegate.m
//  UINavigationControllerSet
//
//  Created by YouXianMing on 15/9/1.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "UIColor+CreateImage.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window                 = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    ViewController         *VC = [[ViewController alloc] init];
    UINavigationController *NC = [[UINavigationController alloc] initWithRootViewController:VC];
    
    [NC.navigationBar setBackgroundImage:[[[UIColor redColor] colorWithAlphaComponent:0.5f] imageWithFrame:CGRectMake(0, 0, 10, 10)]
                          forBarPosition:UIBarPositionAny
                              barMetrics:UIBarMetricsDefault];
    
    [NC.navigationBar setShadowImage:[[UIColor clearColor] imageWithFrame:CGRectMake(0, 0, 10, 10)]];
    
    self.window.rootViewController = NC;
    
    [self.window makeKeyAndVisible];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
复制代码
复制代码
//
//  UIColor+CreateImage.h
//  UITabBarControllerIcon
//
//  Created by YouXianMing on 15/8/31.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIColor (CreateImage)

- (UIImage *)imageWithFrame:(CGRect)frame;

@end
复制代码
复制代码
//
//  UIColor+CreateImage.m
//  UITabBarControllerIcon
//
//  Created by YouXianMing on 15/8/31.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "UIColor+CreateImage.h"

@implementation UIColor (CreateImage)

- (UIImage *)imageWithFrame:(CGRect)frame {

    UIImage *image = nil;
    
    if (self) {
        
        UIView *view         = [[UIView alloc] initWithFrame:frame];
        view.backgroundColor = self;
        
        UIGraphicsBeginImageContext(view.frame.size);
        [[view layer] renderInContext:UIGraphicsGetCurrentContext()];
        image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    
    return image;
}

@end
复制代码

 

细节

 


### 修 iOS 导航栏背景颜色的方法 在 iOS 开发中,自定义 `UINavigationBar` 的背景颜色可以通过多种方式实现。以下是几种常见的方法: #### 方法一:全局设置导航栏背景颜色 可以在应用程序启动时通过 `AppDelegate.m` 文件中的 `didFinishLaunchingWithOptions:` 方法统一设置导航栏背景颜色。具体代码如下所示: ```objective-c [[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]]; ``` 此代码片段会将整个应用内的所有导航栏背景颜色为黄色[^3]。 如果需要指定其他颜色而非标准的颜色常量,则可以使用 RGB 值创建自定义颜色。例如: ```objective-c #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0] [[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)]; ``` 这段代码允许开发者利用宏定义函数 `UIColorFromRGB` 来快速生成基于十六进制值的颜色,并将其应用于导航栏背景[^2]。 #### 方法二:局部调整单个视图控制器导航栏背景色 对于特定场景下仅需更某个页面上的导航栏颜色的情况,可以直接针对对应的 `UIViewController` 设置其所属的 `navigationController.navigationBar.barTintColor` 属性。例如,在某 ViewController 中加入以下代码即可完成操作: ```objective-c self.navigationController.navigationBar.barTintColor = [UIColor blackColor]; ``` #### 方法三:采用图片作为背景 除了单纯变颜色外,还可以加载一张图像并设为导航条底纹图案。这一步骤同样支持两种形式——要么作用于全项目范围,要么限定至单独实例级别。下面展示的是前者做法的一个例子: ```objective-c UIImage *image = [UIImage imageNamed:@"navBackground"]; [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; ``` 以上就是关于如何定制 iOS 应用程序里 Navigation Bar 背景色的主要介绍内容[^1]。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值