写了很长的NavigationController介绍,结果被cnblog吞了,没存档,算了,简单粗暴,直接上如何使用。数组
1.建立3个Controller,继承自UIViewControllerapp
在AppDelegate.h中ide
// // AppDelegate.m // UINavigationController // // Created by lcd on 15/9/11. // Copyright (c) 2015年 lcd. All rights reserved. // #import "AppDelegate.h" #import "FirstViewController.h" #import "SecondViewController.h" #import "ThirdViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
FirstViewController *firstViewController = [[FirstViewController alloc] init];字体
firstViewController.view.backgroundColor = [UIColor redColor];atom
SecondViewController *secondViewController = [[SecondViewController alloc] init];spa
secondViewController.view.backgroundColor = [UIColor whiteColor];3d
ThirdViewController *thirdViewController = [[ThirdViewController alloc] init];code
thirdViewController.view.backgroundColor = [UIColor cyanColor];xml
//建立导航控制器 UINavigationController *navigation = [[UINavigationController alloc] init]; return YES; }
2.使用NavigationController有多种方式blog
//第一种:将须要NavigationController 管理的视图添加进NavigationController 的子视图数组中 UINavigationController *navigation = [[UINavigationController alloc] init]; navigation.viewControllers = @[firstViewController,secondViewController,thirdViewController];
//第二种:逐个添加子控制器 [navigation addChildViewController:firstViewController]; [navigation addChildViewController:secondViewController]; [navigation addChildViewController:thirdViewController];
//第三种:初始化NavigationController时添加自控制器 UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:firstViewController];
3.添加子控制器后,将navigation设置为窗口的根控制器
self.window.rootViewController = navigation;
这样运行,显示的是第三个ViewController,这是由于,NavigationController是以栈的方式管理子视图,如图:
先添加的视图被放在了栈底,显示在咱们面前的是栈顶视图。点击back会把栈顶视图弹出,即为出栈(pop)操做。
NavigationController以 push(入栈)和pop(出栈)管理子视图。
4. NavigationController 结构
4.1导航栏
被NavigationController管理的视图公用一个导航栏,导航栏显示内容由栈顶控制器决定
在ThirdViewController.m中
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationItem.title = @"我是第三个控制器"; }
左上角返回键文字(Back) 由上一个控制器控制
在SecondViewController.m中
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回第二个界面" style:nil target:nil action:nil]; }
运行结果以下图:
4.2 在导航栏添加按钮
UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil]; UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:nil action:nil]; self.navigationItem.rightBarButtonItems = @[button1, button2];
修改右按钮格式
self.navigationItem.titleView = [UIButton buttonWithType:UIButtonTypeContactAdd]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil]; //左按钮同理
隐藏back按钮
[self.navigationItem setHidesBackButton:YES];
4.3 设置导航条样式
[self.navigationController.navigationBar setBarStyle:UIBarStyleDefault];
typedef NS_ENUM(NSInteger, UIBarStyle) {
UIBarStyleDefault = 0,
UIBarStyleBlack = 1,
UIBarStyleBlackOpaque = 1, // Deprecated. Use UIBarStyleBlack
UIBarStyleBlackTranslucent = 2, // Deprecated. Use UIBarStyleBlack and set the translucent property to YES
};
设置导航栏图片
[self.navigationController.navigationBar
setBackgroundImage:[UIImage imageNamed:@"图片名称"] forBarMetrics:UIBarMetricsDefault];
4.4 其余属性
//设置导航条颜色 self.navigationController.navigationBar.barTintColor = [UIColor orangeColor]; //关闭导航条毛玻璃效果 // self.navigationController.navigationBar.translucent = NO; //隐藏导航条 // self.navigationController.navigationBarHidden = YES; //设置导航条内容的颜色 self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; //导航条背景图片 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"pic"] forBarMetrics:UIBarMetricsDefault];
titleTextAttributes
//这是UINavigationBar的一个属性,经过它你能够设置title部分的字体,这个属性定义以下 @property(nonatomic,copy) NSDictionary *titleTextAttributes __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0) UI_APPEARANCE_SELECTOR; //设置title的字体颜色为黄色 NSDictionary *dict = [NSDictionary dictionaryWithObject:[UIColor yellowColor] forKey:UITextAttributeTextColor]; firstViewController.navigationController.navigationBar.titleTextAttributes = dict;
5.界面跳转
从FirstViewController跳转到SceondViewController
在FirstViewController.m中,添加button,并在button事件中添加以下代码:
SecondViewController *secondViewController = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondViewController animated:YES];
从SceondViewController返回FirstViewController:
[self.navigationController popViewControllerAnimated:YES];
注意:从后一个界面跳转回前一个界面,必定要用popViewController,千万不要alloc一个FirstViewController,push到FirstViewController
[self.navigationController popToRootViewControllerAnimated:YES];//这个方法能够跳转回第一个界面