UINavigationController
使用步骤
- 初始化UINavigationController
- 设置UIWindow的rootViewController为UINavigationController
- 根据具体情况,通过push方法添加对应个数的子控制器
// 创建窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 创建导航控制器的跟控制器,也属于导航控制器的子控制器
UIViewController *vc = [[OneViewController alloc] init];
vc.view.backgroundColor = [UIColor redColor];
// 导航控制器也需要一个根控制器
// 默认导航控制器把根控制器的view添加到导航控制器的view上
UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:vc];
NSLog(@"%@",navVc);
// 设置窗口的跟控制器
self.window.rootViewController = navVc;
[self.window makeKeyAndVisible];
UINavigationController的子控制器
- UINavigationController以栈的形式保存子控制器
@property(nonatomic,copy) NSArray *viewControllers; @property(nonatomic,readonly) NSArray *childViewControllers; - 使用push方法能将某个控制器压入栈
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated; -------------------华丽丽的分割线--------------- TwoViewController *vc = [[TwoViewController alloc] init]; vc.view.backgroundColor = [UIColor yellowColor]; // 如果导航控制器调用push,就会把vc添加为导航控制器的子控制器 [self.navigationController pushViewController:vc animated:YES]; - 使用pop方法可以移除控制器
- 将栈顶的控制器移除
-(UIViewController *)popViewControllerAnimated:(BOOL)animated; -------------------华丽丽的分割线--------------- // pop不是马上把控制器销毁, // 回到上一个界面 [self.navigationController popViewControllerAnimated:YES]; - 回到指定的子控制器
-(NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated; -------------------华丽丽的分割线--------------- // 注意:只能返回到栈里面的控制器 [self.navigationController popToViewController:self.navigationController.childViewControllers[0] animated:YES]; - 回到根控制器(栈底控制器)
-(NSArray *)popToRootViewControllerAnimated:(BOOL)animated; -------------------华丽丽的分割线--------------- [self.navigationController popToRootViewControllerAnimated:YES];
- 将栈顶的控制器移除
如何修改导航栏的内容
导航栏的内容由栈顶控制器的navigationItem属性决定
UINavigationItem有以下属性影响着导航栏的内容
- 左上角的返回按钮
@property(nonatomic,retain) UIBarButtonItem *backBarButtonItem; - 中间的标题视图
@property(nonatomic,retain) UIView *titleView; - 中间的标题文字
@property(nonatomic,copy) NSString *title; - 左上角的视图
@property(nonatomic,retain) UIBarButtonItem *leftBarButtonItem; - UIBarButtonItem *rightBarButtonItem 右上角的视图
@property(nonatomic,retain) UIBarButtonItem *rightBarButtonItem;代码
- 左上角的返回按钮
左右按钮
// 左边按钮 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"左边" style:UIBarButtonItemStyleDone target:self action:@selector(btnClick)]; // 导航条右边按钮 // 在iOS7之后默认会把导航条上面的按钮渲染成蓝色 UIImage *image = [UIImage imageNamed:@"navigationbar_friendsearch"]; // 通过代码告诉苹果不要渲染图片 image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:nil action:nil];自定义按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; [btn setImage:[UIImage imageNamed:@"navigationbar_friendsearch"] forState:UIControlStateNormal]; [btn setImage:[UIImage imageNamed:@"navigationbar_friendsearch_highlighted"] forState:UIControlStateHighlighted]; // 导航条上面的内容位置不能由开发者决定,开发者只能控制尺寸 // 控件的尺寸由图片决定 // 仅仅是设置尺寸 [btn sizeToFit]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];