一,設定導航欄title的字型顔色,有兩種方式
(1)直接自定義title
//第一種: 自定義title
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
title.text = @"自定義title";
title.textAlignment = NSTextAlignmentCenter;
title.textColor = [UIColor redColor];
title.font=[UIFont systemFontOfSize:20];
self.navigationItem.titleView = title;
(2)設定系統自帶的title的屬性值
//第二種:設定系統自帶title的屬性值
self.title=@"設定系統自帶title";
NSDictionary *titleAtt=[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:20],NSFontAttributeName,[UIColor blackColor],NSForegroundColorAttributeName,nil];
[self.navigationController.navigationBar setTitleTextAttributes:titleAtt];
二,設定導航欄上的左右按鈕
(1) 第一種: 系統自帶方式
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"left" style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.leftBarButtonItem = leftItem;
(2) 第二種:自定義 方式(常使用這種方式)
UIButton *rightButn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 40)];
[rightButn setBackgroundColor:[UIColor yellowColor]];
[rightButn setTitle:@"自定義" forState:UIControlStateNormal];
[rightButn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:rightButn];
三,設定導航欄背景顔色
//設定導航欄背景顔色
self.navigationController.navigationBar.barTintColor=[UIColor cyanColor];
四,修改backBarButtonItem中的文字
//通過設定navigationItem的backBarButtonItem可以直接更換文字,【注意,要在父視圖的Controller中設定】
UIBarButtonItem *backItem=[[UIBarButtonItem alloc]initWithTitle:@"fanhui" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem=backItem; 效果如下:
//自定義傳回按鈕
UIImage *backButtonImage = [[UIImage imageNamed:@"fanhui.jpg"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
//将傳回按鈕的文字position設定不在螢幕上顯示
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];
五、導航欄顔色漸變
如果在單元格滑動的時候,觸發導航欄顔色的變化,需要實作UIScrollViewDelegate(或者UITableViewDelegate)協定
#pragma mark-UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self.navigationController.navigationBar setBackgroundImage:[self imageWithBgColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:self.tableView.contentOffset.y / 100]] forBarMetrics:UIBarMetricsDefault];
}
//延伸————》顔色轉為圖檔的工具性方法
-(UIImage *)imageWithBgColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}