由于项目需要最近多navigation的操作颇多,为了便于以后使用也方便大家,所以在这里一一记录下来(以375*667为例)
1.设置navigationBar透明(系统默认)
代码:
[self.navigationController.navigationBar setTranslucent:YES];
效果:可以使得navigationBar呈现半透明,类似UIVisualEffectView的效果,并且可以增加tableView的滑动区域(注意是滑动区域而不是滚动区域)。意思是
可以设置
self.tableView= [[UITableViewalloc]initWithFrame:self.view.bounds];
实际效果为tableview的实际显示区域只有 667 - 64;但是他的有效滚动区域却是667
/*
New behavior on iOS 7.
Default is YES.
You may force an opaque background by setting the property to NO.
If the navigation bar has a custom background image, the default is inferred
from the alpha values of the image—YES if it has any pixel with alpha < 1.0
If you send setTranslucent:YES to a bar with an opaque custom background image
it will apply a system opacity less than 1.0 to the image.
If you send setTranslucent:NO to a bar with a translucent custom background image
it will provide an opaque background for the image using the bar's barTintColor if defined, or black
for UIBarStyleBlack or white for UIBarStyleDefault if barTintColor is nil.
>*/
根据上面说法,当设置背景图的时候,只要背景图的alpha<1 ,那么translucent就会被设置为YES。
2.关于automaticallyAdjustsScrollViewInsets
代码:
self.automaticallyAdjustsScrollViewInsets=YES;
效果:在有navigationBar的状态下,scrollView会默认被下推64px,就是因为默认设置self.automaticallyAdjustsScrollViewInsets=YES 。 这样的状态在navigationBar是透明的情况下是可以设置为NO并且有效的,但是在translucent = NO的情况下不能改变。
3.设置navigationBar全透明
1.用rgbColor创建一张透明的图片设置为背景图
[self.navigationController.navigationBarsetBackgroundImage:[UIImagecreateImageWithColor:[UIColorclearColor]]forBarMetrics:UIBarMetricsDefault];
2.找到并设置navigationBar的imageView线隐藏
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
return (UIImageView *)view;
}
for (UIView *subview in view.subviews) {
UIImageView *imageView = [self findHairlineImageViewUnder:subview];
if (imageView) {
return imageView;
}
}
return nil;
}
4.滚动tableView、scrollView,让navigationbar的隐藏的2种方法
方法一
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
if (scrollView == self.containerView) {
if(velocity.y>0)
{
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
else
{
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
}
}
方法二
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//<0的话是正常
if (_tableView.contentOffset.y <= -64) {//显示
self.navigationController.navigationBar.frame = CGRectMake(0, 20, 375, 44);
[self.navigationController setNavigationBarHidden:NO animated:NO];
}
else if (_tableView.contentOffset.y < 0){
self.navigationController.navigationBar.frame = CGRectMake(0, 20 - (_tableView.contentOffset.y + 64) , 375, 44);
}else{
self.navigationController.navigationBar.frame = CGRectMake(0, -44, 375, 44);
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
//-64 是正常的contentoff
}
5.navigationbar的颜色渐变
颜色渐变也可以参考navigationbar隐藏的方案二方法,实现起来还是比较方便的