收集iOS--UINavigationController的常用
2016-12-24 本文已影响43人
TenMios
导航控制器 (UINavigationController )
高度:44
组成:UINavigationBar(上) ,NavigationView(中), Navigation toolbar(下) 组成
简述:可以管理一系列显示层次性信息的场景。 第一个场景显示有关特定主题的高级视图,第二个场景用于进一步描述,第三个场景再进一步描述。
过程 : 它会创建一个视图控制器“栈” 栈底是根视图控制器。当用户在场景之间进行切换时候,一次将视图控制器压入栈中,并且当前场景的视图控制器位于栈顶。要返回到上一级,导航控制器将弹出栈顶的控制器,从而回到它下面的控制器。
术语: push (压入) pop(弹出)来描述导航控制器
present dismiss
modal 出来的控制器 是没有导航栏的
-(void)buttonClick{ // 弹出窗口的两种方式
#import "MIONavigationController.h" // 第一种要 导入头文件
1. 有导航栏
MIONavigationController *nav =[[MIONavigationController alloc] initWithRootViewController:[[MIOSearchVC alloc]init]];
[self presentViewController:nav animated:YES completion:nil];
2. 没有导航栏
MIOSearchVC *svc = [[MIOSearchVC alloc]init];
[self.navigationController presentViewController:svc animated:YES completion:nil];
}
用户输入信息的view 结束后自动回复到之前的view 用 presentModalViewController 比较适合
NavigationBar -> Navigationitem -> (UIBarButtonItem) leftBarButtonItem (左边)
rightBarButtonItem(右边)
backBarButtonItem
3. 实例:创建一个导航项
UINavigationItem *navigationItem =[ [ UINavigationItem alloc] init]
4. 创建一个左边的按钮
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initwithTitle:@“编辑” style:UIBarButtonItemStylePlain target:self action:@selector(beginEdit:)];
5. 设置导航栏内容
navigationItem.title = “hello”;
把左边按钮添加到导航栏集合中
[navigationItem setLeftBarButtonItem : rightButton];
或者self.navigationItem.rightBarButtonItem = rightButton
titletextAttributes 可以设置title 部分字体
可以通过titleVIew 属性 指定一个定制的titleview 指定的frame 不要显示出界
Navigation toolbar(下)
显示工具栏
self.navigationController.toolbarHidden = NO ;
■ UINavigationController的使用步骤
➢ 初始化UINavigationController
➢ 设置UIWindow的rootViewController为UINavigationController
■ 根据具体情况,通过push方法添加对应个数的子控制器
UINavigationController的子控制器
■ UINavigationController以栈的形式保存子控制器
@property(nonatomic,copy) NSArray *viewControllers;
@property(nonatomic,readonly) NSArray *childViewControllers;
■ 使用push方法能将某个控制器压入栈
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
■ 使用pop方法可以移除控制器
➢ 将栈顶的控制器移除
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
➢ 回到指定的子控制器
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
➢ 回到根控制器(栈底控制器)
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;
如何修改导航栏的内容
■ 导航栏的内容由栈顶控制器的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;
应用实例:
UINavigationController *nav =[[UINavigationController alloc] init];
//设置导航栏背景图片
[ nav.navigationBar setBackgroundImage:[UIImage imageNamed:@“@"mine-setting-icon"”] forBarMetrics: UIBarMetricsDefault]
//设置导航栏内容
self.navigationItem.title = @"我的";
//设置随机背景色
self.view.backgroundColor =[UIColor colorWithRed:arc4random_uniform(100)/100.0 green:arc4random_uniform(100)/100.0 blue:arc4random_uniform(100)/100.0 alpha:1.0];
//(只设置一个的情况)
//设置导航栏右边的按钮
UIButton *settingButton =[UIButton buttonWithType:UIButtonTypeCustom];
[settingButton setBackgroundImage:[UIImage imageNamed:@"mine-setting-icon"] forState:UIControlStateNormal];
[settingButton setBackgroundImage:[UIImage imageNamed:@"mine-setting-icon-click"] forState:UIControlStateHighlighted];
settingButton.size = settingButton.currentBackgroundImage.size;
[settingButton addTarget:self action:@selector(settingClick) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:settingButton];
——————————————————————————————————————————————————————
//(设置两个情况)
UIButton *settingButton =[UIButton buttonWithType:UIButtonTypeCustom];
[settingButton setBackgroundImage:[UIImage imageNamed:@"mine-setting-icon"] forState:UIControlStateNormal];
[settingButton setBackgroundImage:[UIImage imageNamed:@"mine-setting-icon-click"] forState:UIControlStateHighlighted];
settingButton.size = settingButton.currentBackgroundImage.size;
[settingButton addTarget:self action:@selector(settingClick) forControlEvents:UIControlEventTouchUpInside];
UIButton *settingButton2 =[UIButton buttonWithType:UIButtonTypeCustom];
[settingButton2 setBackgroundImage:[UIImage imageNamed:@"mine-setting-icon"] forState:UIControlStateNormal];
[settingButton2 setBackgroundImage:[UIImage imageNamed:@"mine-setting-icon-click"] forState:UIControlStateHighlighted];
settingButton2.size = settingButton2.currentBackgroundImage.size;
[settingButton2 addTarget:self action:@selector(setting2Click) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItems = @[
[UIBarButtonItem alloc]initWithCustomView:settingButton],
[UIBarButtonItem alloc]initWithCustomView:setting2Button]
];
——————————————————————————————————————————————————————
6. 隐藏导航栏
self.navigationController.navigationBar.hidden = YES;
self.navigationController.navigationBarHidden = YES;
[self.navigationController setNavigationBarHidden:YES animated:NO];
#import "UIBarButtonItem+MIOExtension.h"
7. 快速创建 按钮
self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"MainTagSubIcon"highImage:@"MainTagSubIconClick" target:self action:@selector(tagClick)];