1.navigationItem設(shè)置
navigationItem可以設(shè)置:
屬性 | 效果 |
---|---|
title | 標(biāo)題 |
titleView | 標(biāo)題視圖(UIView) |
leftBarButtonItem | 左邊的按鈕(UIBarButtonItem) |
leftBarButtonItems | 左邊的按鈕們(UIBarButtonItem數(shù)組) |
rightBarButtonItem | 右邊的按鈕 |
rightBarButtonItems | 右邊的按鈕們 |
backBarButonItem | 返回按鈕(UIBarButtonItem) |
prompt | 描述 |
1.1 title/leftBarButtonItem/rightBarButtonItems設(shè)置
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor orangeColor]];
self.navigationController.navigationBar.translucent = NO;
CGFloat w = [UIScreen mainScreen].bounds.size.width;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 0, w-16, 220)];
imageView.image = [UIImage imageNamed:@"hy.jpg"];
[self.view addSubview:imageView];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"item1" style:UIBarButtonItemStylePlain target:self action:@selector(btnClicked:)];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"item2" style:UIBarButtonItemStylePlain target:self action:@selector(btnClicked:)];
self.navigationItem.title = @"Hello World";
self.navigationItem.leftBarButtonItem = item1;
self.navigationItem.rightBarButtonItems = @[item1,item2];
//leftItems/rightItems的顏色
self.navigationController.navigationBar.tintColor = [UIColor redColor];
//設(shè)置NavigationBar背景顏色
self.navigationController.navigationBar.barTintColor = [UIColor greenColor];
//設(shè)置title的文字顏色
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor yellowColor]};
需要注意的是 navigationController和 navigationBar和 navigationItem三者的關(guān)系,這一點(diǎn)在下一小節(jié)論述.
還有,要記得如何設(shè)置title的顏色,如何設(shè)置items的顏色,如何設(shè)置navigationBar的顏色.
效果:
1.2 prompt設(shè)置
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor orangeColor]];
self.navigationController.navigationBar.translucent = NO;
CGFloat w = [UIScreen mainScreen].bounds.size.width;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 0, w-16, 220)];
imageView.image = [UIImage imageNamed:@"hy.jpg"];
[self.view addSubview:imageView];
self.navigationItem.title = @"Hello World";
//描述信息,navigationBar高度增加30,由44變成74
self.navigationItem.prompt = @"this is prompt";
}
prompt屬性用來添加描述信息,賦值后,navigationBar高度增加30,由44變成74
效果
1.3 titleView設(shè)置
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor orangeColor]];
self.navigationController.navigationBar.translucent = NO;
CGFloat w = [UIScreen mainScreen].bounds.size.width;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 0, w-16, 220)];
imageView.image = [UIImage imageNamed:@"hy.jpg"];
[self.view addSubview:imageView];
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
UILabel *titleLabe = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
titleLabe.text = @"newtitle";
titleLabe.textAlignment = NSTextAlignmentCenter;
titleLabe.textColor = [UIColor redColor];
[titleView addSubview:titleLabe];
titleView.backgroundColor = [UIColor yellowColor];
self.navigationItem.titleView = titleView;
}
注意
如果需要在 self.navigationItem.titleView 中添加子控件,需要先實(shí)例化一次self.navigationItem.titleView:
self.navigationItem.titleView = [[UIView alloc] init];
不然無論你忘titleView中放多少控件,都是顯示不出來的.
效果
1.4 自定義返回按鈕
-(void)btnClicked:(UIBarButtonItem *)sender{
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor purpleColor];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"自定義返回" style:UIBarButtonItemStylePlain target:vc action:nil];
vc.navigationItem.leftBarButtonItem = backItem;
[self.navigationController pushViewController:vc animated:YES];
}
自定義返回按鈕,最簡單的方法就是,設(shè)置push出了的新控制器的leftBarButtonItem,當(dāng)leftBarButtonItem設(shè)置好之后, backBarButonItem就不顯示了.
2. navigationBar和 navigationItem的關(guān)系
(該小節(jié)參考:http://blog.csdn.net/luoyeffcs/article/details/16106707/)
先看一下這些類的繼承關(guān)系,可以看清很多問題.
類 | 繼承自 |
---|---|
UINavigationController | UIViewController |
UINavigationBar | UIView |
UINavigationItem | NSObject |
UIBarButtonItem | UIBarItem |
UIBarItem | NSObject |
問題1
navigationItem和navigationController都是UIViewController的屬性,而UINavigationController的父類就是UIViewController,所以會出現(xiàn):
self.navigationController.navigationController
和
self.navigationController.navigationItem
這種現(xiàn)象(self指UIViewController).而這應(yīng)該是被忽略的屬性(沒啥亂用,容易混淆).
navigationItem是UIViewController的屬性,
正確設(shè)置navigationItem:self. navigationItem
navigationBar是UINavigationController的屬性,
正確設(shè)置navigationBar :self.navigationController.navigationBar
問題2
self.title 和self.navigationItem.title 和self.tabBarItem.title
由于UIViewController本身包含navigationItem和tabBarItem飘言,所以可以單獨(dú)對其title賦值碍扔,self.title會重寫另外兩個(gè)的值难裆,只是提供的一種便利方法。
當(dāng)self.title = @"123"時(shí), self.navigationItem.title 的值和 self.tabBarItem.title 的值都是@"123"
問題3
UIBarItem和 UIBarButtonItem 還有 UINavigationItem的關(guān)系
UIBarItem類是一個(gè)可以放置在Bar之上的所有小控件類的抽象類沉眶。繼承了該基類所有子類在外觀上類似于一個(gè)Button,它們都有一個(gè)標(biāo)題安券,圖片悉盆,動作以及目標(biāo).
** UIBarButtonItem繼承自UIBarItem,專門用來放在UIToolbar 或者 UINavigationBar的特殊button.基本行為跟button是一樣的。
** UINavigationItem包含了當(dāng)前頁面導(dǎo)航欄上需要顯示的全部信息,包括:
title, prompt, titleView, leftBarButtonItem, leftBarButtonItems, rightBarButtonItem, rightBarButtonItems, backBarButonItem
問題4
navigationBar的各種顏色設(shè)置
//設(shè)置標(biāo)題
self.navigationItem.title = @"Hello World";
//設(shè)置左邊的按鈕
self.navigationItem.leftBarButtonItem = item1;
//設(shè)置右面的按鈕 們
self.navigationItem.rightBarButtonItems = @[item1,item2];
//設(shè)置leftItems/rightItems的顏色
self.navigationController.navigationBar.tintColor = [UIColor redColor];
//設(shè)置NavigationBar背景顏色
self.navigationController.navigationBar.barTintColor = [UIColor greenColor];
//設(shè)置NavigationBar背景圖片
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
//設(shè)置title的文字顏色
self.navigationController.navigationBar.titleTextAttributes @{NSForegroundColorAttributeName:[UIColor yellowColor]};