iOS开发-自定义UINavigationBar样式

属性修改

  • 修改标题内容
// 两个方法都可以(在对应的ViewController中使用)
self.title = @"First";
self.navigationItem.title = @"First";
  • 修改标题颜色、字体、阴影效果等样式
// 设置Title的字体颜色黄色、字体大小24、lightGray颜色阴影
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowColor:[UIColor lightGrayColor]];
[shadow setShadowOffset:CGSizeMake(1.0, 0.0)];
NSDictionary *titleTextAttDict = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor yellowColor], NSForegroundColorAttributeName, [UIFont systemFontOfSize:24], NSFontAttributeName, shadow, NSShadowAttributeName, nil];
[self.navigationController.navigationBar setTitleTextAttributes:titleTextAttDict];
  • 修改标题的垂直位置
// 垂直向下10个Pt
[self.navigationController.navigationBar setTitleVerticalPositionAdjustment:10 forBarMetrics:UIBarMetricsDefault];
  • 修改NavigationBar背景颜色
//修改NavigationBar背景颜色为orangeColor
[self.navigationController.navigationBar setBarTintColor:[UIColor orangeColor]];
  • 修改NavigationBar的背景图片
//修改NavigationBar的背景图片
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Bar_bg"] forBarMetrics:UIBarMetricsDefault];
  • 修改NavigationBar毛玻璃效果
// 设置为没有毛玻璃效果,view Controller背景为lightGray
[self.navigationController.navigationBar setTranslucent:NO];
没有毛玻璃效果
有毛玻璃效果
  • 修改NavigationBar红色毛玻璃效果
[self.navigationController.navigationBar setTranslucent:YES];
[self.navigationController.navigationBar setBackgroundColor:[UIColor redColor]];
毛玻璃红色,view controller灰色
  • 设置返回按钮颜色
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
默认颜色
修改后的颜色
  • 修改返回按钮图标
[self.navigationController.navigationBar setBackIndicatorImage:[UIImage imageNamed:@"back"]];
[self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"back"]];
修改前
修改后
  • 隐藏阴影分割线
[self.navigationController.navigationBar setClipsToBounds:YES];
修改前
修改后
  • 修改阴影分割线颜色
// 用红色的图片设置阴影分割线,和背景图片一起设置才有效果
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Bar_bg"] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:@"border"]];
修改前
修改后

UINavigationItem设置

iOS开发-自定义UINavigationBar样式_第1张图片
  • UIViewController持有自己的UINavigationItem
  • UINavigationBar通过items持有于viewControllers一一对应的UINavigationItem栈

  • 自定义左右按钮/titileView

UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"更多" style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
UIBarButtonItem *leftBackBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem *leftCloseBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"close"] style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem *leftSpaceBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
leftSpaceBarButtonItem.width = -16;
self.navigationItem.leftBarButtonItems = @[leftSpaceBarButtonItem, leftBackBarButtonItem, leftCloseBarButtonItem];
    
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"share"] style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
[titleView setBackgroundColor:[UIColor blackColor]];
self.navigationItem.titleView = titleView;

位置根据具体情况进行调整

你可能感兴趣的:(iOS开发-自定义UINavigationBar样式)