屬性修改
- 修改標題內容
// 兩個方法都可以(在對應的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]];
- 設置返回按鈕顏色
[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設置
- 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;
位置根據具體情況進行調整