iOS开发-玩玩渐变导航栏
2016-06-07 15:32
337 查看
最近博主对渐变导航栏进行了改进,原来的是只有在头部部分400以内会改变,现在当在中部上下滚动时也会有类似的功能,和百度贴吧的功能类似,需要的请查看博客链接:http://blog.csdn.net/CodingFire/article/details/53705318
看到有人说渐变的导航栏,所以就随便来写写,根据渐变原理,应该是控件在滚动的时候,根据偏移量来设置导航栏背景的透明度。
这就简单了,废话不多说,直接上代码:
这里透明度变化的维度自己控制,我这边在400以内变化,超出的透明度都为1,看到别人写的隐藏导航条的方法,这里拿出来分享一下:
第二个知识点,拿到导航栏背景图,其实导航栏背景图是导航栏的第一个子集,所以拿到所有的导航栏子集,取第一位:
这里要注意,不能写成
否则nav上的标题按钮也会消失,而实用:
隐藏的只是背景图片,并不会把nav上面的其他子集隐藏。附上下载地址:https://github.com/codeliu6572/NavClear
看到有人说渐变的导航栏,所以就随便来写写,根据渐变原理,应该是控件在滚动的时候,根据偏移量来设置导航栏背景的透明度。
这就简单了,废话不多说,直接上代码:
// // ViewController.m // NavClear // // Created by 刘浩浩 on 16/6/7. // Copyright © 2016年 CodingFire. All rights reserved. // #import "ViewController.h" @interface ViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource> { UIImageView *barImageView; UITableView *myTableView; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor=[UIColor whiteColor]; self.navigationController.navigationBar.barTintColor=[UIColor orangeColor]; self.title=@"This is my title!"; self.navigationController.navigationBar.tintColor = [UIColor blackColor]; myTableView=[[UITableView alloc]initWithFrame:self.view.bounds]; myTableView.delegate=self; myTableView.dataSource=self; [self.view addSubview:myTableView]; UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 270)]; imageView.image=[UIImage imageNamed:@"1.png"]; myTableView.tableHeaderView=imageView; barImageView = self.navigationController.navigationBar.subviews.firstObject; } #pragma mark - UITableViewDelaget -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 40; } -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { return 44; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"]; if (!cell) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; } cell.textLabel.text=[NSString stringWithFormat:@"%ld",indexPath.row]; return cell; } -(void)scrollViewDidScroll:(UIScrollView *)scrollView { self.navigationController.navigationBar.alpha=1-scrollView.contentOffset.y/400; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
这里透明度变化的维度自己控制,我这边在400以内变化,超出的透明度都为1,看到别人写的隐藏导航条的方法,这里拿出来分享一下:
//这里的图片是导航条的背景图片,相当于设置了nil [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; //这里的图片是下面那条线,如果不设置会看到64像素位置的那条黑线 self.navigationController.navigationBar.shadowImage = [UIImage new]; //还有一种最简单的方法 [self.navigationController setNavigationBarHidden:YES animated:YES];
第二个知识点,拿到导航栏背景图,其实导航栏背景图是导航栏的第一个子集,所以拿到所有的导航栏子集,取第一位:
barImageView = self.navigationController.navigationBar.subviews.firstObject;
这里要注意,不能写成
self.navigationController.navigationBar.alpha=1-scrollView.contentOffset.y/400;
否则nav上的标题按钮也会消失,而实用:
barImageView = self.navigationController.navigationBar.subviews.firstObject;
隐藏的只是背景图片,并不会把nav上面的其他子集隐藏。附上下载地址:https://github.com/codeliu6572/NavClear
相关文章推荐
- iOS 空中升级蓝牙设备代码思想
- iOS 源代码分析 --- MBProgressHUD
- iOS开发——XML/JSON数据解析
- ios9和ios10的新特性
- iOS编译FFmpeg及使用实现视频播放
- nagios安装管理
- iOS 面向切面编程与typhoon框架
- iOS应用支持IPV6,就那点事儿
- iOS 开发:绘制像素到屏幕
- iOS开发拓展篇-XMPP简单介绍
- iOS开发拓展篇—应用之间的跳转和数据传
- iOS数据持久化 归档 plist文件
- CocoaPods的安装及第三方类库的使用
- ios开发之OC基础-ios开发学习路线图
- iOS7(iPhone4)button不能改变button的title
- iOS生成二维码利用CoreImage
- shareSDK关于通信的设置
- iOS开发拓展篇—音频处理(音乐播放器6)
- ios开发中类方法和实例方法self区别
- iOS开发拓展篇—xib中关于拖拽手势的潜在错误