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;
}
[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透明属性)
方法二:
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.viewaddGestureRecognizer: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]方法
方法一
如果控制器是由导航控制管理,设置状态栏的样式时,要在导航控制器里设置
-(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
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
- ios之导航渐变---/导航透明/隐藏导航栏以及手势返回遇到的问题,状态栏
- IOS 导航栏(状态栏)透明遇到的一些问题
- IOS自定义系统导航栏:导航栏背景色、状态栏样式以及是否隐藏、拦截系统导航栏的返回点击事件
- IOS自定义系统导航栏:导航栏背景色、状态栏样式以及是否隐藏、拦截系统导航栏的返回点击事件
- iOS导航栏返回手势
- iOS 导航栏的手势返回
- 导航栏透明渐变
- 导航栏渐变透明
- 导航栏透明渐变(iOS)
- iOS 关于滑动返回隐藏导航栏的问题
- iOS 导航栏的隐藏以及右滑返回的问题
- Ionic3 透明导航栏、渐变导航栏
- iOS自定制导航栏返回按钮或者隐藏导航栏pop手势失效解决方案
- iOS 隐藏导航栏或状态栏
- iOS 导航栏渐变
- iOS 导航栏隐藏出现的问题
- 透明状态栏和导航栏的解决方案
- iOS导航栏隐藏的情况下设置状态栏颜色
- C4
- 编译c++程序x64/x86 运行出现 应用程序无法正常启动0xc000007b_学习笔记
- linux在下软件安装-jdk和tomcat安装
- Python :字符流中第一个不重复的字符
- 【Linux】中多路转接之poll
- ios之导航渐变---/导航透明/隐藏导航栏以及手势返回遇到的问题,状态栏
- C# 日期格式化
- IO流
- 51Nod X^2 Mod P
- python_函数(二)
- CART(Classification And Regression Tree)算法原理详解
- UDP协议
- 适配器模式
- 面向对象编程之单态设计模式