一、UINavigationController基本屬性
寫這么多文章居然沒人給我評(píng)論幾句。。。
UINavigationController,導(dǎo)航控制器,用來管理多個(gè)視圖控制器
它就是管理視圖控制器的控制器
從此可以在Appdelegate.m的launch函數(shù)中先聲明導(dǎo)航欄控制器,再將它作為視圖控制器的爸爸,window的根視圖控制器,像下面這樣
//初始化一個(gè)表視圖控制器
RootTableViewController *rootTableVC = [[RootTableViewController alloc]init];
//初始化一個(gè)導(dǎo)航控制器,把表視圖控制器加上去
UINavigationController *navigationC = [[UINavigationController alloc]initWithRootViewController:rootTableVC];
//設(shè)置導(dǎo)航控制器為window的根視圖控制器
self.window.rootViewController = navigationC;
導(dǎo)航欄有個(gè)重要的屬性,它的不透明效果,它會(huì)影響坐標(biāo)原點(diǎn)的位置
self.navigationController.navigationBar.translucent
當(dāng)開啟時(shí)屏幕左上角為原點(diǎn)
當(dāng)關(guān)閉時(shí),導(dǎo)航欄的左下角為原點(diǎn)
iOS7以后默認(rèn)為YES
下面說導(dǎo)航控制器的各種屬性
//初始化一個(gè)視圖控制器
RootViewController *rootVC = [[RootViewController alloc]init];
//初始化導(dǎo)航欄視圖控制器,并加載一個(gè)根視圖控制器
UINavigationController *navigationC = [[UINavigationController alloc]initWithRootViewController:rootVC];
//設(shè)置導(dǎo)航欄視圖控制器為根視圖控制器
self.window.rootViewController = navigationC;
//標(biāo)題
self.navigationItem.title = @"block首頁(yè)";
//設(shè)置背景色
self.navigationController.navigationBar.backgroundColor = [UIColor blackColor];
//標(biāo)題顏色
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil];
//導(dǎo)航欄顏色
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:109/255.0 green:211/255.0 blue:206/255.0 alpha:1];
//導(dǎo)航欄元素顏色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
//也可以設(shè)置中間的標(biāo)題為視圖
UISegmentedControl *qqSegmentedControl = [[UISegmentedControl alloc]initWithItems:@[@"消息",@"電話"]];
self.navigationItem.titleView = qqSegmentedControl;
//添加左側(cè)按鈕,使用系統(tǒng)的按鈕樣式
UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
self.navigationItem.leftBarButtonItem = leftBarBtn;
//添加右側(cè)按鈕使用定義文字的初始化方式
UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc]initWithTitle:@"去第二頁(yè)" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
self.navigationItem.rightBarButtonItem = rightBarBtn;
//還可以使用圖片給或者View給bar按鈕初始化
//UIBarButtonItem *test1 = [UIBarButtonItem alloc]initWithImage:<#(nullable UIImage *)#> style:<#(UIBarButtonItemStyle)#> target:<#(nullable id)#> action:<#(nullable SEL)#>
//UIBarButtonItem *test2 = [UIBarButtonItem alloc]initWithCustomView:<#(nonnull UIView *)#>
//可以給右側(cè)或者左側(cè)添加按鈕組,把按鈕放入數(shù)組
//self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:test,test1, nil];
系統(tǒng)的導(dǎo)航欄按鈕樣式有很多,下圖很不錯(cuò),來自友人
二、UINavigationController控制頁(yè)面跳轉(zhuǎn)
導(dǎo)航欄控制器的主要作用是控制頁(yè)面跳轉(zhuǎn)
現(xiàn)在我們創(chuàng)建了兩個(gè)視圖控制器:
RootViewController
SecondViewController
1.在AppDelegate.m中創(chuàng)建RootViewController對(duì)象和導(dǎo)航控制器
//創(chuàng)建根視圖
RootViewController *rootVC = [[RootViewController alloc]init];
//創(chuàng)建導(dǎo)航控制器
UINavigationController *navigationC = [[UINavigationController alloc]initWithRootViewController:rootVC];
//添加到window根視圖控制器
self.window.rootViewController = navigationC;
2.在RootViewController.m中給NavigationController添加按鈕,給按鈕添加跳轉(zhuǎn)方法并實(shí)現(xiàn)
- (void)viewDidLoad {
[super viewDidLoad];
//創(chuàng)建右側(cè)按鈕
UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc]initWithTitle:@"前往第二頁(yè)" style:UIBarButtonItemStylePlain target:self action:@selector(jumpToSecondVC)];
//添加按鈕到導(dǎo)航欄
self.navigationItem.rightBarButtonItem = rightBarBtn;
}
-(void)jumpToSecondVC
{
//創(chuàng)建secondVC,要導(dǎo)入SecondViewController的.h文件
SecondViewController *secondVC = [[SecondViewController alloc]init];
//將secondVC入棧,棧頂?shù)囊晥D為正在顯示的視圖
[self.navigationController pushViewController:secondVC animated:YES];
}
3.在SecondViewController.m中添加一個(gè)返回按鈕,點(diǎn)擊可以返回rootVC
系統(tǒng)會(huì)默認(rèn)在左上角添加一個(gè)返回按鈕,我們可以重新添加
- (void)viewDidLoad {
[super viewDidLoad];
//創(chuàng)建右側(cè)按鈕
UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc]initWithTitle:@"回到首頁(yè)" style:UIBarButtonItemStylePlain target:self action:@selector(jumpToRootVC)];
//添加按鈕到導(dǎo)航欄
self.navigationItem.rightBarButtonItem = rightBarBtn;
// Do any additional setup after loading the view.
}
-(void)jumpToRootVC
{
//將當(dāng)前的視圖控制器出棧
[self.navigationController popViewControllerAnimated:YES];
//也可直接返回根視圖控制器
[self.navigationController popToRootViewControllerAnimated:YES];
//或者返回指定層的視圖控制器
//可以得到當(dāng)前棧內(nèi)的視圖,存在數(shù)組 self.navigationController.viewControllers里
[self.navigationController popToViewController: self.navigationController.viewControllers[0]animated:YES];
}
實(shí)現(xiàn)了效果也比較簡(jiǎn)單,點(diǎn)擊首頁(yè)的“前往第二頁(yè)”可以跳到第二頁(yè)
點(diǎn)擊第二頁(yè)的“首頁(yè)”或者“回到首頁(yè)”可以調(diào)回首頁(yè),其中“首頁(yè)”按鈕是系統(tǒng)自動(dòng)加的,“回到首頁(yè)”是我們自己寫的下一節(jié)要專門總結(jié)一下界面跳轉(zhuǎn)的同時(shí)的傳值問題