iOS之导航NavigationController_导航栏iOS Developer

自定义导航栏样式(背景、标题、图片、返回按钮、功能按钮)

2016-05-25  本文已影响898人  Vinc

一、更改导航栏的背景

[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:20/255.0 green:155/255.0 blue:213/255.0 alpha:1.0]];  
// 需要额外设置 info.plist 中的 View controller-based status bar appearance 这个设置方法可以在AppDelegate中设置,全局可以生效
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];

二、改变导航栏标题属性

方法1
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]]; 
方法2
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

三、在导航栏使用背景图片

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"logo.png"] forBarMetrics:UIBarMetricsDefault]; 
[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"coupon"] stretchableImageWithLeftCapWidth:2 topCapHeight:2] forBarMetrics:UIBarMetricsDefault];

四、使用图片作为导航栏标题

self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"coupon"]];

五、添加多个栏按钮项目

UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
NSArray *itemsArr = @[shareItem,cameraItem];
self.navigationItem.rightBarButtonItems = itemsArr;

六、自定义后退按钮的文字和颜色

方法1 注意,要在父视图的Controller中设置
ViewController *vc = [ViewController new];
[self.navigationController pushViewController:vc animated:YES];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = item;
方法2
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

七、自定义返回按钮

UIImage *backButtonImage = [[UIImage imageNamed:@"fanhui.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 0, 0)];  
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];  
//将返回按钮的文字position设置不在屏幕上显示  
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];    
上一篇下一篇

猜你喜欢

热点阅读