自定义的 UINavigationBar
子类,允许在 iOS 6/7 上使用渐变色导航栏。
platform :ios
pod 'CRGradientNavigationBar'
...
运行 pod install
以安装依赖。
将头文件导入到使用渐变色导航栏的任何地方
#import "CRGradientNavigationBar.h"
initWithNavigationBarClass:toolbarClass:
方法与 UINavigationController
一起使用UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[CRGradientNavigationBar class] toolbarClass:nil];
NSArray
设置渐变色(您可以设置无限颜色)UIColor *firstColor = [UIColor colorWithRed:255.0f/255.0f green:42.0f/255.0f blue:104.0f/255.0f alpha:1.0f];
UIColor *secondColor = [UIColor colorWithRed:255.0f/255.0f green:90.0f/255.0f blue:58.0f/255.0f alpha:1.0f];
NSArray *colors = [NSArray arrayWithObjects:firstColor, secondColor, nil];
setBarTintGradientColors:
方法设置渐变色[[CRGradientNavigationBar appearance] setBarTintGradientColors:colors];
setTranslucent:
方法设置(YES)或不要设置(NO)透明值[[navigationController navigationBar] setTranslucent:NO];
您可以更改默认透明度值(0.5f),只需在 CRGradientNavigationBar.m
文件中更改此行即可
static CGFloat const kDefaultOpacity = 0.5f;
完整示例
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[CRGradientNavigationBar class] toolbarClass:nil];
UIColor *firstColor = [UIColor colorWithRed:255.0f/255.0f green:42.0f/255.0f blue:104.0f/255.0f alpha:1.0f];
UIColor *secondColor = [UIColor colorWithRed:255.0f/255.0f green:90.0f/255.0f blue:58.0f/255.0f alpha:1.0f];
NSArray *colors = [NSArray arrayWithObjects:firstColor, secondColor, nil];
// or NSArray *colors = [NSArray arrayWithObjects:(id)UIColorFromRGB(0xf16149).CGColor, (id)UIColorFromRGB(0xf14959).CGColor, nil];
[[CRGradientNavigationBar appearance] setBarTintGradientColors:colors];
[[navigationController navigationBar] setTranslucent:NO]; // Remember, the default value is YES.
DemoViewController *viewController = [[DemoViewController alloc] init];
[navigationController setViewControllers:@[viewController]];
[self.window setRootViewController:navigationController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
setBarTintColor:
方法[[CRGradientNavigationBar appearance] setBarTintColor:UIColorFromRGB(0xEE695D)];
透明度演示 1
透明度演示 2
CRGradientNavigationBar 采用了 MIT 许可证发布。请参阅 LICENSE。
Christian Roman
联系方式:[email protected]
Twitter:http://twitter.com/chroman