UINavigationController ( 导航控制器 )
来源:互联网 发布:c语言 字符串统计函数 编辑:程序博客网 时间:2024/05/29 16:04
1.基本概念
UINavigationController ( 导航控制器 )
是UIViewController的子类 所以它可以直接赋值给window的rootViewController
但它同时也是其它ViewController的容器。
// 父类 UIView
@property(nonatomic,readonly) UINavigationBar *navigationBar;
//父类 NSObject
@property(nonatomic,readonly,strong) UINavigationItem *navigationItem;
navigationBar 和 navigationItem 是导航控制器最重要的两个属性
navigationItem 为UIViewController 的属性
navigationBar的内容 一般根据栈顶控制器(当前展示控制器)的navigationItem的设置来显示
UINavigationItem 有以下属性影响着导航栏的内容
//左上角的返回按钮
@property(nonatomic,retain) UIBarButtonItem *backBarButtonItem;
//中间的标题视图
@property(nonatomic,retain) UIView *titleView;
//中间的标题文字
@property(nonatomic,copy) NSString *title;
//左上角的视图
@property(nonatomic,retain) UIBarButtonItem *leftBarButtonItem;
//右上角的视图
@property(nonatomic,retain) UIBarButtonItem *rightBarButtonItem;
2.系统导航栏自定义
UINavigationBar
//字体以及图标颜色
@property(null_resettable, nonatomic,strong) UIColor *tintColor;
//背景颜色 默认nil
@property(nullable, nonatomic,strong) UIColor *barTintColor;
//透明度
UINavigationBar 的父类 是uiview 直接设置view 的属性alpha即可
self.navigationController.navigationBar.alpha = 0.5;
//背景图片
(void)setBackgroundImage:(UIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics;
(void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics;
//标题位置
- (void)setTitleVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics;
//修改系统导航栏高度
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
CGRect rect = self.navigationController.navigationBar.frame;
rect.size.height = 300;
bar.frame = rect;
}
导航栏中间部位 titleView 属性为UIView 所以可以自定义成任何视图
导航栏左右视图部分 leftBarButtonItem rightBarButtonItem 中 UIBarButtonItem 默认为系统定义的样式
可以通过以下初始化方式 来定义图片或者标题
- (instancetype)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;
- (instancetype)initWithImage:(UIImage *)image landscapeImagePhone:(UIImage *)landscapeImagePhone style:(UIBarButtonItemStyle)style target:(id)target action:(nullable SEL)action;
- (instancetype)initWithTitle:(nullable NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;
也可以通过 - (instancetype)initWithCustomView:(UIView *)customView;方法 进行完全自定义
3.导航控制器对控制器的管理和切换
UINavigationController以栈的形式保存子控制器
@property(nonatomic,copy) NSArray *viewControllers;
使用push方法能将某个控制器压入栈
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
使用pop方法可以移除控制器
将栈顶的控制器移除
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
回到指定的子控制器
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
回到根控制器(栈底控制器)
(NSArray *)popToRootViewControllerAnimated:(BOOL)animated;
可右划返回属性
interactivePopGestureRecognizer.enabled = YES ;
4.自定义导航栏
由于系统导航栏的很多属性都是只读的, 所以在很多常用APP 他们都选择了 自定义导航栏
第一步 需要隐藏当前系统导航栏
self.navigationController.navigationBar.hidden = YES;
第二步 新建navgationBar
由于想要最大限度去实现各种可能性 所以放弃了写UINavigationBar 分类的想法 而选择了继承于UIView 手写出各个控件
下面利用Reveal 工具看一下系统导航栏的结构
UINavgationBar 分四个组件
1) 为了设置本身背景颜色和背景图片而存在的 _UINavigationBarBackground
_UINavigationBarBackground 其内两个组件 UIView 和 UIImageView
2) UINavgationItemView 也就是平时引用到的titleView 默认情况下里面只包含一个UILabel 当 UIViewController 调用 self.title = @“test”;时,此 UILabel.text = @“test”;
3)图中左侧Cancel 按钮 通过以下代码 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:nil]; 设置
其中包含一个UILabel
4) 图中右侧 Camera 通过以下代码
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];设置
- UINavigationController(导航控制器)
- UINavigationController ( 导航控制器 )
- 导航控制器(UINavigationController)(UINavigationController)
- IOS中的UINavigationController(导航控制器)
- 导航视图控制器UINavigationController
- 导航控制器 ( UINavigationController )
- 导航控制器UINavigationController
- UINavigationController导航控制器详解
- UINavigationController导航控制器…
- UINavigationController导航控制器…
- UINavigationController导航控制器…
- UINavigationController 导航控制器
- 导航控制器UINavigationController
- 导航控制器:UINavigationController
- 自定义UINavigationController(导航控制器)
- swift UINavigationController 导航控制器
- UINavigationController 导航视图控制器
- UINavigationController 导航控制器
- Java--TreeMap源码解读
- swift基础2
- 功能强大的Xcode辅助工具Faux Pas:帮你找到各种隐形的bug
- 26、Java调用Oracle存储过程?
- USB Mass Storage大容量存储的基本知识
- UINavigationController ( 导航控制器 )
- CF_5B_CenterAlignment
- [Unity官方文档翻译]Importing Assets 如何向unity导入资源
- BZOJ 1086([SCOI2005]王室联邦-树的划分)
- ios swift 归档解档遇到的坑 3-30
- ubuntu 15.04 通过3306端口远程连接mysql
- C++组合应用之strtok字符串分割、数组平分算法、动态数组函数传参
- android手机安全性测试手段
- PCL系列——从深度图像(RangeImage)中提取NARF关键点