本文主要是介绍iOS之导航渐变---/导航透明/隐藏导航栏以及手势返回遇到的问题,状态栏颜色和背景颜色,tabbarItem角标、导航背景颜色,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
// 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;
}
[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;
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;则需要触发滑动手势
这是因为导航条是属于导航控制器的而并不是导航控制器的每个子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]方法
========= =状态栏. http://www.jianshu.com/p/53a7d93fb418======================方法一
如果控制器是由导航控制管理,设置状态栏的样式时,要在导航控制器里设置
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
方法二
-(BOOL)prefersStatusBarHidden{
return YES;//隐藏状态栏
}
方法三
// 统一设置状态栏的样式
// xcode5以上,创建的项目,默认的话,这个状态栏的样式由控制器决定,这是要配置plist文件为NO;
[UIApplication sharedApplication].statusBarStyle =UIStatusBarStyleLightContent;//这个方法子啊IOS9 失效 了;
View controller-based status bar appearance项设为YES,则View controller对status bar的设置优先级高于application的设置。
为NO则以application的设置为准,view controller的prefersStatusBarHidden方法无效,是根本不会被调用的。
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的话就更简单啦~在控制器直接写这个方法就好。
在info.plist中View controller-based status bar appearance 设置为YES,在控制器中设置优先。
- (UIStatusBarStyle)preferredStatusBarStyle {return UIStatusBarStyleLightContent;
}
状态栏背景色:
iOS 13之前,可以通过valueForKey 获取UIApplication的statusBar,因为UIApplication是单例,因此,在iOS 12,通过: [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]拿到的statusBar永远是同一个对象。不行可以打印出内存地址看下就很清楚了。iOS 13之后,因为苹果不允许使用KVC的valueForKey访问私有属性。通过上面的代码可以多看点,每次进来都调用 alloc:init的方法,重新生成一个statusBar;然后添加到UIApplication的keyWindow上,再设置背景颜色。因此这个方法多次调用就会创建多份statusBar,造成内存开销不说,如果设置为透明,根本不能起开效果。
解决办法:在iOS 13 之后,创建一个statuBar单例对象。
#import <Foundation/Foundation.h>NS_ASSUME_NONNULL_BEGIN@interface XBSNavBarManager : UIView
+(XBSNavBarManager *)sharedStausManager;
+(void)setStausManager;
+(void)cancelStausManager;
@endNS_ASSUME_NONNULL_END#import "XBSNavBarManager.h"@implementation XBSNavBarManager
+(XBSNavBarManager *)sharedStausManager{static UIView* statusBar =nil;static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{if (@available(iOS 13.0, *)) {statusBar = [[UIView alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame];} else {// Fallback on earlier versions}});return (XBSNavBarManager*)statusBar;
}
+(void)setStausManager{if (@available(iOS 13.0, *)) {[XBSNavBarManager sharedStausManager].backgroundColor=[UIColor whiteColor];[[UIApplication sharedApplication].keyWindow addSubview: [XBSNavBarManager sharedStausManager]];}else{//简单粗暴KVC获取到状态栏ViewUIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]){//设置状态栏背景色statusBar.backgroundColor = [UIColor whiteColor];}}
}
+(void)cancelStausManager{if (@available(iOS 13.0, *)) {[XBSNavBarManager sharedStausManager].backgroundColor=[UIColor clearColor];[[UIApplication sharedApplication].keyWindow addSubview: [XBSNavBarManager sharedStausManager]];}else{//简单粗暴KVC获取到状态栏ViewUIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]){//设置状态栏背景色statusBar.backgroundColor = [UIColor clearColor];}}
}
@end使用:
在主控制器中:
-(void)viewWillAppear:(BOOL)animated{[super viewWillAppear:animated];[MXNavigationBarManager setStatusBarStyle:UIStatusBarStyleLightContent];//状态栏白色[XBSNavBarManager cancelStausManager];//取消状态栏背景色,在下一级vc中设置状态栏背景色[self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];//取消导航颜色}- (void)viewWillDisappear:(BOOL)animated{[super viewDidDisappear:animated];[MXNavigationBarManager setStatusBarStyle:UIStatusBarStyleDefault];//设置状态黑色,默认色}在下一级控制器中:
-(void)viewDidAppear:(BOOL)animated{[super viewDidAppear:animated];[self.navigationController.navigationBar setBackgroundColor:[UIColor whiteColor]];[XBSNavBarManager setStausManager];
}
----------------tabbarItem角标-------------
[self.navigationController.tabBarItem setBadgeColor:[UIColor redColor]];
[self.navigationController.tabBarItem setBadgeValue:@"1"];
导航背景颜色:
-(void)viewWillAppear:(BOOL)animated{[super viewWillAppear:animated];[self.navigationController.navigationBar setTranslucent:NO];//取消半透明[self.navigationController.navigationBar setBarTintColor:[UIColor blueColor]];//导航背景色,会延伸到状态栏
}
[self.navigationController.navigationBar setBackgroundColor:[UIColor whiteColor]];仅仅设置导航颜色,不包含状态栏。
这篇关于iOS之导航渐变---/导航透明/隐藏导航栏以及手势返回遇到的问题,状态栏颜色和背景颜色,tabbarItem角标、导航背景颜色的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!