如self.navigationController.navigationBar開頭或者使用appearance的好像都是對(duì)所有導(dǎo)航欄有效的幔欧。
設(shè)置背景顏色拂苹、文字顏色
[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:114.0/255 green:64.0/255 blue:11.0/255 alpha:1.0]];
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
或者
[[UINavigationBar appearance]setBarTintColor:[UIColor redColor]];
[[UINavigationBar appearance]setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
或者在導(dǎo)航欄使用背景圖片
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBg"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
當(dāng)然灿意,使用[UINavigationBar appearance]來設(shè)置背景圖也是可以的
去掉導(dǎo)航欄下面那條黑線
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBg"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
或者
[self.navigationController.navigationBar.layer setMasksToBounds:YES];
設(shè)置導(dǎo)航欄返回按鈕圖片并不顯示文字
選擇我們要使用的圖片替換返回按鈕的圖片,然后使返回按鈕的標(biāo)題不顯示
UIImage *backButton = [UIImage imageNamed:@"back.png"];
方法一:使用自己的圖片替換原來的返回圖片
self.navigationController.navigationBar.backIndicatorImage = [backButton imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.navigationController.navigationBar.backIndicatorTransitionMaskImage = [backButton imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
方法二:
UIImage *backButtonImage = [backButton resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 0)];//可拉伸區(qū)域
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
// 將返回按鈕的文字position設(shè)置不在屏幕上顯示
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];
寫一個(gè)UINavigationController的**類別**處理返回按鈕的標(biāo)題:
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item{
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
item.backBarButtonItem = back;
return YES;
}