UINavigationController组成分析
UINavigationController
的包含组成
- 包括导航条
UINavigationBar(self.navigationController.navigationBar)
- 内容区域(
self.navigationController.viewControllers
) - 工具条
UIToolbar
(self.navigationController.toolbar)
//显示工具条
self.navigationController.toolbarHidden = NO;
1.内容区域的显示问题
1.1控制器View的影响
导航条透明和不透明,器内容默认是全局(0)开始的
就是系统会默认打开一下属性
//导航条的默认设置的c全局
- (void)translucentAndAll {
//translucent这个属性在iOS6或者6之前默认是NO,ios7z之后默认是yes
//导航条是否透明
self.navigationController.navigationBar.translucent = YES;
//这个属性默认是UIRectEdgeAll 内容的扩展布局
self.edgesForExtendedLayout = UIRectEdgeAll;
//扩展是否包括不透明的Bars 默认是NO
self.extendedLayoutIncludesOpaqueBars = NO;
//这个属性已经被contentInsetAdjustmentBehavior 代替
//contentInsetAdjustmentBehavior 这个属性是UIScrollView的
self.automaticallyAdjustsScrollViewInsets = YES;
}
- 更改内容区域显示(64)
//更改到64开始
- (void)translucentAnd64{
//这个属性默认是UIRectEdgeAll 内容的扩展布局
self.edgesForExtendedLayout = UIRectEdgeNone;
//扩展是否包括不透明的Bars 默认是NO
// self.extendedLayoutIncludesOpaqueBars = YES;
}
1.2-控制器View上布局ScrollView的影响
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(50, 0, 200, 200)];
scrollView.backgroundColor = [UIColor yellowColor];
scrollView.contentSize = CGSizeMake(0, 500);
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 200)];
label.backgroundColor = [UIColor redColor];
label.text = @"添加scrollView上的Label";
[scrollView addSubview:label];
[self.view addSubview:scrollView];
_scrollView = scrollView;
1- 设置导航条不透明
self.navigationController.navigationBar.translucent = NO;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(50, 0, 200, 200)];
scrollView.backgroundColor = [UIColor yellowColor];
scrollView.contentSize = CGSizeMake(0, 500);
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 200)];
label.backgroundColor = [UIColor redColor];
label.text = @"添加scrollView上的Label";
[scrollView addSubview:label];
[self.view addSubview:scrollView];
_scrollView = scrollView;
1.3-导航控制器管理的控制器
导航控制的已经指定根视图控制器,
push
一次到twoVc
CCTwoVC *twoVc = [[CCTwoVC alloc] init];
self.navigationController pushViewController:twoVc animated:YES];
在
push
一次到threeVC
CCThreeVC *threeVC = [[CCThreeVC alloc] init];
[self.navigationController pushViewController:threeVC animated:YES];
我们在
threeVC
的viewDidLoad
断点打印
1.3.1-导航控制器管理的子控制器更改替换
我们知道子控制器存放在一个数组
viewControllers
这个属性里面
@property(nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers;
可以更改赋值
- 移除控制器栈里面的一个控制器
- 从上面的图中,我们知道,自控制器有3个
(lldb) po self.navigationController.viewControllers
<__NSArrayI 0x600000401920>(
<ViewController: 0x7fd56e514fa0>,
<CCTwoVC: 0x7fd56e704fc0>,
<CCThreeVC: 0x7fd56e41d7b0>
)
- 我们移除最后一个控制器
NSMutableArray *array = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
//移除最后一个
[array removeLastObject];
//赋值
self.navigationController.viewControllers = array;
//返回栈顶控制器
[self.navigationController popViewControllerAnimated:YES];
- 当我们移除以后一个控制器后,
TwoVC
就是topViewController
,所以pop
返回到TwoVC
,因为pop
是返回到栈顶控制器,由下图对这个方法的描述也可以知道
- 如果上述的代码,移除2次控制器,那么在
pop
返回的时候就是 栈顶控制器根视图控制器rootVC
NSMutableArray *array = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
//移除最后一个
[array removeLastObject];
//移除一个
[array removeLastObject];
//赋值
self.navigationController.viewControllers = array;
//返回栈顶控制器
[self.navigationController popViewControllerAnimated:YES];
- 跳转到指定的控制器
NSMutableArray *array = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
//跳转返回到指定的控制器
[self.navigationController popToViewController:[array firstObject] animated:YES];
2.导航条
####- 透明度
//默认是半透明的
//self.navigationController.navigationBar.translucent = YES;
//我们试着导航条的透明 只需要设置一个 透明的背景图片
UIImage *alphaImage = [self imageWithColor:alphaColor];
//修改导航条背景图片
[self.navigationController.navigationBar setBackgroundImage:alphaImage forBarMetrics:UIBarMetricsDefault];
- (UIImage *)imageWithColor:(UIColor *)color
{
// 描述矩形
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
// 开启位图上下文
UIGraphicsBeginImageContext(rect.size);
// 获取位图上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 使用color演示填充上下文
CGContextSetFillColorWithColor(context, [color CGColor]);
// 渲染上下文
CGContextFillRect(context, rect);
// 从上下文中获取图片
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
// 结束上下文
UIGraphicsEndImageContext();
return theImage;
}
- 导航条黑线的清除
- 方法一
//清除黑线 裁剪超出的部分
self.navigationController.navigationBar.clipsToBounds = YES;
- 方法二
//设置阴影图片
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
- 方法三 遍历 NavBar的子控件找到黑线,设置透明
//递归函数
- (UIImageView *)findBlackLineImageView:(UIView*)view{
if([view isKindOfClass:[UIImageView class]] && view.frame.size.height <= 1){
return (UIImageView*)view;
}
NSArray *viewAry = view.subviews;
for (int i = 0; i < viewAry.count; i++) {
UIView *tmpV = [self findBlackLineImageView:viewAry[i]];
if (tmpV) {
return (UIImageView*)tmpV;
}
}
return nil;
}
- 利用系统的item设置间隔
//Item之间的间距
-(void)setupMoreNavItemWithSpace {
NSMutableArray *itemsBar = [NSMutableArray array];
UIBarButtonItem *rightItem1 = [[UIBarButtonItem alloc] initWithTitle:@"测试1" style:UIBarButtonItemStylePlain target:nil action:nil];
//自动调节
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
spaceItem.width = 10;
UIBarButtonItem *rightItem2 = [[UIBarButtonItem alloc] initWithTitle:@"测试2" style:UIBarButtonItemStylePlain target:nil action:nil];
[itemsBar addObject:rightItem1];
[itemsBar addObject:spaceItem];
[itemsBar addObject:rightItem2];
self.navigationItem.rightBarButtonItems = itemsBar;
}
- 导航条的隐藏
//MARK:--导航条的隐藏
- (void)setupNavHidden{
//方式一 对navigationBar的frame做处理
self.navigationController.navigationBarHidden = YES;
//方式二 这个中方式是View的hidden属性的处理
self.navigationController.navigationBar.hidden = YES;
}