// CZNavTableViewController.h
// 导航栏渐变透明效果
#import
@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
@interface UINavigationBar (alpha)
//动态添加的UIview添加到UINavigationBar上
@property (nonatomic ,strong)UIView *alphaView;
- (void)alphaNavigationBarView:(UIColor *)color;
@end
#import "UINavigationBar+alpha.h"
#import
@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;
}
[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];
}
#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
- //视图将要显示时隐藏
- -(void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
- [self.navigationController.navigationBar setShadowImage:[UIImage new]];
- }
- //视图将要消失时取消隐藏
- -(void)viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear:animated];
- [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
- [self.navigationController.navigationBar setShadowImage:nil];
- }
[objc] view plain copy
- @property (nonatomic, weak) UIImageView *lineView;
- //视图加载完成获取到导航栏最下面的黑线
- - (void)viewDidLoad {
- [super viewDidLoad];
- //获取导航栏下面黑线
- _lineView = [self getLineViewInNavigationBar:self.navigationController.navigationBar];
- }
- //视图将要显示时隐藏
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- _lineView.hidden = YES;
- self.navigationController.navigationBar.translucent = YES;
- self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
- }
- //视图将要消失时取消隐藏
- - (void)viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear:animated];
- _lineView.hidden = NO;
- self.navigationController.navigationBar.translucent = NO;
- self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
- }
- //找到导航栏最下面黑线视图
- - (UIImageView *)getLineViewInNavigationBar:(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 getLineViewInNavigationBar:subview];
- if (imageView) {
- return imageView;
- }
- }
- return nil;
- }
=======手势返回遇到的问题
禁止手势返回
-(void)popGestureChange:(UIViewController *)vc enable:(BOOL)enable{
if ([vc.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
//遍历所有的手势
for (UIGestureRecognizer *popGesture in vc.navigationController.interactivePopGestureRecognizer.view.gestureRecognizers) {
popGesture.enabled = enable;
}
}
}
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;
id traget =self.navigationController.interactivePopGestureRecognizer.delegate;
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizeralloc]initWithTarget:tragetaction:nil];
[self.view addGestureRecognizer:pan];
id target = self.navigationController.interactivePopGestureRecognizer.delegate;
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
pan.delegate = self;
[self.view addGestureRecognizer:pan];
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{}
这是因为导航条是属于导航控制器的而并不是导航控制器的每个子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)animated的animated参数一致([self.navigationController setNavigationBarHidden:YES animated:animated]、[self.navigationController setNavigationBarHidden:NO animated:animated]),因为当界面是动画显示出来(如push、pop)的时候-(void)viewWillAppear:(BOOL)animated的animated参数本来就会是YES,而当界面不是动画显示出来的时候-(void)viewWillAppear:(BOOL)animated的animated参数会是NO而这个时候我们也不需要动画的隐藏导航条。
当然也可以不用在B的viewWillAppear方法中而在A的- (void)viewWillDisappear:(BOOL)animated中调用[self.navigationController setNavigationBarHidden:NO animated:YES]方法
方法一
如果控制器是由导航控制管理,设置状态栏的样式时,要在导航控制器里设置
-(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;
}
[self.navigationController.tabBarItem setBadgeColor:[UIColor redColor]];
[self.navigationController.tabBarItem setBadgeValue:@"1"];