ios之导航渐变---/导航透明/隐藏导航栏以及手势返回遇到的问题,状态栏

来源:互联网 发布:fanuc pmc编程说明书 编辑:程序博客网 时间:2024/05/01 17:11

//  CZNavTableViewController.h

//  导航栏渐变透明效果

#import <UIKit/UIKit.h>


@interface CZNavTableViewController :UITableViewController


@end

================

//  CZNavTableViewController.m

//  导航栏渐变透明效果

//


#import "CZNavTableViewController.h"

#import "UINavigationBar+alpha.h"


@interface CZNavTableViewController ()


@end


@implementation CZNavTableViewController


- (void)viewDidLoad {

    [superviewDidLoad];


    

    //

//    [self.navigationController.navigationBar setBackgroundColor:[UIColor redColor]];

    

    //

//    [self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];

    

    

//    [self.navigationController.navigationBar setTintColor:[UIColor redColor]];

    

//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];

    

    

}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

//当前的y

   CGFloat OffsetY = scrollView.contentOffset.y;

    

    UIColor *color = [UIColorredColor];

    

    CGFloat alpha = (30 +64 - OffsetY)/64;

    if (OffsetY >30) {


        [self.navigationController.navigationBaralphaNavigationBarView:[colorcolorWithAlphaComponent:alpha]];

    }else

    {

        [self.navigationController.navigationBaralphaNavigationBarView:[colorcolorWithAlphaComponent:1]];

    }



}

#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

#warning Incomplete implementation, return the number of sections

    return0;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

#warning Incomplete implementation, return the number of rows

    return0;

}

=================

#import <UIKit/UIKit.h>


@interface UINavigationBar (alpha)


//动态添加的UIview添加到UINavigationBar

@property (nonatomic ,strong)UIView *alphaView;


- (void)alphaNavigationBarView:(UIColor *)color;


@end


===========

#import "UINavigationBar+alpha.h"

#import <objc/runtime.h>


@implementation UINavigationBar (alpha)

static char alView;


-(UIView *)alphaView

{

    returnobjc_getAssociatedObject(self, &alView);

}



-(void)setAlphaView:(UIView *)alphaView

{

    objc_setAssociatedObject(self, &alView, alphaView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}


//通过这个方法去改变颜色和透明度

-(void)alphaNavigationBarView:(UIColor *)color

{

    if (!self.alphaView) {

        //设置一张图片,如果不设置,颜色不纯

        

        [selfsetBackgroundImage:[UIImagenew]forBarMetrics:UIBarMetricsDefault];

    

        //创建

        self.alphaView = [[UIViewalloc]initWithFrame:CGRectMake(0, -20,[UIScreenmainScreen].bounds.size.width , 64)];

        

        //添加到navigationbar

        [selfinsertSubview:self.alphaViewatIndex:0];

    }

    [self.alphaViewsetBackgroundColor:color];


}



@end


===============方法二==========
在需要设置导航透明的方法中调用下面的方法;

- (void)setNavigationController


{

    [self.navigationController.navigationBar setBackgroundImage:[selfcreateImageWithColor:[UIColorclearColor]]forBarMetrics:UIBarMetricsDefault];

    [self.navigationController.navigationBar setShadowImage:[selfcreateImageWithColor:[UIColorclearColor]]];

    [self.navigationController.navigationBar setTintColor:[UIColorwhiteColor]];

    [self.navigationController.navigationBar setTranslucent:YES];

}



-(UIImage *) createImageWithColor: (UIColor *) color

{

    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context =UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [colorCGColor]);

    CGContextFillRect(context, rect);

    

    UIImage *theImage =UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return theImage;

}


=======方法三=====
clearo是一张透明的PNG图片;

[self.navigationController.navigationBarsetBackgroundImage:[UIImageimageNamed:@"clearo"]forBarMetrics:UIBarMetricsDefault];

    [self.navigationController.navigationBar setShadowImage:[selfcreateImageWithColor:[UIColorclearColor]]];//这个是导航下面的阴影线,如果用的是透明背景,这个可以不用显示。



*******************隐藏导航栏
方法一:

注意这里一定要用动画的方式隐藏导航栏,这样在使用滑动返回手势的时候效果最好,和上面动图一致.这样做有一个缺点就是在切换tabBar的时候有一个导航栏向上消失的动画.

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    

    [self.navigationController setNavigationBarHidden:YES animated:YES];

}


- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];

    

    [self.navigationController setNavigationBarHidden:NO animated:YES];

}




方法二:调用navigation的代理方法
self.navigationcontroller.delegate=self;

#pragma mark - UINavigationControllerDelegate

// 将要显示控制器-----隐藏导航

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

    // 判断要显示的控制器是否是自己

    BOOL isShowHomePage = [viewControllerisKindOfClass:[selfclass]];

    

    [self.navigationControllersetNavigationBarHidden:isShowHomePageanimated:YES];

}




==========去掉导航栏下部的黑线

去掉导航栏self.navigationController.navigationBar下默认黑线。

方法一:(会影响导航栏的translucent透明属性

[objc] view plain copy
  1. //视图将要显示时隐藏  
  2. -(void)viewWillAppear:(BOOL)animated  
  3. {  
  4.     [super viewWillAppear:animated];  
  5.       
  6.     [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];  
  7.     [self.navigationController.navigationBar setShadowImage:[UIImage new]];  
  8. }  
  9.   
  10. //视图将要消失时取消隐藏  
  11. -(void)viewWillDisappear:(BOOL)animated  
  12. {  
  13.     [super viewWillDisappear:animated];  
  14.       
  15.     [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];  
  16.     [self.navigationController.navigationBar setShadowImage:nil];  
  17. }  
方法二:

[objc] view plain copy
  1. @property (nonatomic, weak) UIImageView *lineView;  
  2.   
  3. //视图加载完成获取到导航栏最下面的黑线  
  4. - (void)viewDidLoad {  
  5.     [super viewDidLoad];  
  6.       
  7.     //获取导航栏下面黑线  
  8.     _lineView = [self getLineViewInNavigationBar:self.navigationController.navigationBar];  
  9. }  
  10.   
  11. //视图将要显示时隐藏  
  12. - (void)viewWillAppear:(BOOL)animated  
  13. {  
  14.     [super viewWillAppear:animated];  
  15.       
  16.     _lineView.hidden = YES;  
  17.     self.navigationController.navigationBar.translucent = YES;  
  18.     self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];  
  19. }  
  20.   
  21. //视图将要消失时取消隐藏  
  22. - (void)viewWillDisappear:(BOOL)animated  
  23. {  
  24.     [super viewWillDisappear:animated];  
  25.       
  26.     _lineView.hidden = NO;  
  27.     self.navigationController.navigationBar.translucent = NO;  
  28.     self.navigationController.navigationBar.barTintColor = [UIColor blackColor];  
  29. }  
  30.   
  31. //找到导航栏最下面黑线视图  
  32. - (UIImageView *)getLineViewInNavigationBar:(UIView *)view  
  33. {  
  34.     if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {  
  35.         return (UIImageView *)view;  
  36.     }  
  37.       
  38.     for (UIView *subview in view.subviews) {  
  39.         UIImageView *imageView = [self getLineViewInNavigationBar:subview];  
  40.         if (imageView) {  
  41.             return imageView;  
  42.         }  
  43.     }  
  44.       
  45.     return nil;  
=======手势返回遇到的问题
参考:http://blog.csdn.net/yz_lby/article/details/49082131

http://www.jianshu.com/p/7706a8a33b2d

http://blog.csdn.net/jasonblog/article/details/28282147

http://blog.csdn.net/lvxiangan/article/details/51042806
从iOS7.0后苹果自带了侧滑返回手势功能interactivePopGestureRecognizer,但是有时我们自定义返回按钮或者隐藏了导航栏,侧滑返回就会失效;

//当自定义返回按钮时,手势返回失效,用下面的设置能开启手势滑动

- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    

    // 手势右滑返回---开启

    if ([self.navigationControllerrespondsToSelector:@selector(interactivePopGestureRecognizer)]) {

        self.navigationController.interactivePopGestureRecognizer.delegate = nil;

    }

    

}


//

   //手势右滑返回禁用

self.navigationController.interactivePopGestureRecognizer.enabled=NO;

    //手势右滑返回开启

    self.navigationController.interactivePopGestureRecognizer.enabled=YES;

----*******如果上面的手势返回不能禁止的话,可以在viewDidload中直接加入下面这几句话:

 id traget =self.navigationController.interactivePopGestureRecognizer.delegate;

    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizeralloc]initWithTarget:tragetaction:nil];

    [self.viewaddGestureRecognizer:pan];


*********如果返回手势失效的话:可以重写手势返回的方法:
setp1:需要获取系统自带滑动手势的target对象
id target = self.navigationController.interactivePopGestureRecognizer.delegate;
setp2:创建全屏滑动手势~调用系统自带滑动手势的target的action方法
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
step3:设置手势代理~拦截手势触发
pan.delegate = self;
step4:别忘了~给导航控制器的view添加全屏滑动手势
[self.view addGestureRecognizer:pan];
step5:将系统自带的滑动手势禁用
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
steo6:还记得刚刚设置的代理吗?下面方法什么时候调用?在每次触发手势之前都会询问下代理,是否触发。
这个方法就是拦截手势触发的方法.
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{}
return NO;则不需要触发滑动手势
return YES;则需要触发滑动手势


一种情景,在导航控制器中有个界面A导航条需要隐藏而A的下一级界面B则需要显示导航条。我刚开始的解决方案是在A的viewWillAppear方法中设置self.navigationController.navigationBar.hidden = YES;而在B的viewWillAppear方法中设置self.navigationController.navigationBar.hidden = NO。本来以为没什么问题,结果无意中发现在B界面使用iOS7以上系统的手势滑动返回A时,B界面刚有一点偏移A的viewWillAppear方法就已经调用了导致A界面尚未完全显示到窗口导航条就已经消失了;

这是因为导航条是属于导航控制器的而并不是导航控制器的每个子VC都有一个属于自己的导航条。

而我实际想要的效果却是在手势滑动返回A界面途中导航条随着B界面一起偏移;



那就是在A的viewWillAppear方法中不要使用self.navigationController.navigationBar.hidden = YES;这个方法而应该使用[self.navigationControllersetNavigationBarHidden:YESanimated:YES]这个方法,相应的B的viewWillAppear方法中也不要使用self.navigationController.navigationBar.hidden = NO这个方法而应该使用[self.navigationControllersetNavigationBarHidden:NOanimated:YES]这个方法。注意animated这个参数一定要设置为YES,因为使用[self.navigationController setNavigationBarHidden:YES animated:YES]之所以能达到上图这种我们想要的效果就是因为有这个动画,而这个动画效果就是导航条随着导航控制器的子VC的界面一起偏移。当然也可以把animated这个参数设置为和

-(void)viewWillAppear:(BOOL)animatedanimated参数一致([self.navigationController setNavigationBarHidden:YES animated:animated]、[self.navigationController setNavigationBarHidden:NO animated:animated]),因为当界面是动画显示出来(如push、pop)的时候-(void)viewWillAppear:(BOOL)animatedanimated参数本来就会是YES,而当界面不是动画显示出来的时候-(void)viewWillAppear:(BOOL)animatedanimated参数会是NO而这个时候我们也不需要动画的隐藏导航条。

当然也可以不用在B的viewWillAppear方法中而在A的- (void)viewWillDisappear:(BOOL)animated中调[self.navigationController setNavigationBarHidden:NO animated:YES]方法


==========状态栏http://www.jianshu.com/p/53a7d93fb418
参考:https://www.cnblogs.com/Free-Thinker/p/6478770.html

方法一

如果控制器是由导航控制管理,设置状态栏的样式时,要在导航控制器里设置


-(UIStatusBarStyle)preferredStatusBarStyle{


    returnUIStatusBarStyleLightContent;

}


方法二

-(BOOL)prefersStatusBarHidden{

    return YES;//隐藏状态栏


}


方法三

    // 统一设置状态栏的样式

    // xcode5以上,创建的项目,默认的话,这个状态栏的样式由控制器决定,这是要配置plist文件


    

    [UIApplication sharedApplication].statusBarStyle =UIStatusBarStyleLightContent;//这个方法子啊IOS9 失效 了;


1 . 根据app主色调设置BaseViewController 的preferredStatusBarStyle,根据主色调如果想设置白色状态栏样式,那么只需要在BaseViewController写下面这个方法即可。

- (UIStatusBarStyle)preferredStatusBarStyle {  // 如果app绝大多数页面要设置黑色样式,可以不写此方法,因为默认样式就是黑色的。  // return UIStatusBarStyleDefault;    // 白色样式    return UIStatusBarStyleLightContent;}

2 .如果想在继承自BaseViewController的控制器里改变状态栏样式,比如白色换成黑色,只需要重写一下父类的方法即可。

- (UIStatusBarStyle)preferredStatusBarStyle {    return UIStatusBarStyleDefault;}

3 .特殊情况,当继承自BaseViewController的控制器里出现了导航栏时,此时通过preferredStatusBarStyle方法改变状态栏样式可能不管用,这个时候就需要用到下面这个方法。

-(void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    // 这样设置状态栏样式是黑色的    //[self.navigationController.navigationBar setBarStyle:UIBarStyleDefault];    // 这样设置状态栏样式是白色的    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];}

4 .上面3种情况都是说BaseViewController,那么如果没有BaseViewController的话呢?哈哈,没有BaseViewController的话就更简单啦~在控制器直接写这个方法就好。

- (UIStatusBarStyle)preferredStatusBarStyle {    return UIStatusBarStyleLightContent;}


作者:顶起_那片天
链接:http://www.jianshu.com/p/53a7d93fb418
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。






原创粉丝点击