iOS开发-玩玩渐变导航栏
来源:互联网 发布:淘宝哪家种子靠谱 编辑:程序博客网 时间:2024/05/29 02:19
最近博主对渐变导航栏进行了改进,原来的是只有在头部部分400以内会改变,现在当在中部上下滚动时也会有类似的功能,和百度贴吧的功能类似,需要的请查看博客链接:http://blog.csdn.net/CodingFire/article/details/53705318
看到有人说渐变的导航栏,所以就随便来写写,根据渐变原理,应该是控件在滚动的时候,根据偏移量来设置导航栏背景的透明度。
这就简单了,废话不多说,直接上代码:
//// 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
0 0
- iOS开发-玩玩渐变导航栏
- ios开发:导航栏navigationbar背景渐变
- iOS开发Navigation 导航栏渐变
- iOS开发 - 渐变导航栏终极版
- iOS 导航栏渐变
- iOS 开发中导航栏渐变的两种方法
- iOS开发实现导航栏透明度渐变效果
- iOS 导航栏颜色渐变
- iOS开发 导航栏渐变 图片下拉放大 导航栏移动 仿QQ 微博 知乎导航栏
- 导航栏透明渐变(iOS)
- iOS 超Easy实现 渐变导航栏
- iOS 超Easy实现 渐变导航栏
- iOS 导航栏标题文字颜色渐变
- iOS设置导航栏渐变颜色
- iOS 超 Easy 实现 渐变导航栏
- ios中的导航栏渐变效果
- IOS tableView滑动设置导航栏透明度渐变 类似QQ空间导航栏渐变
- ios之导航渐变---/导航透明/隐藏导航栏以及手势返回遇到的问题,状态栏
- Android studio 实用插件
- 错误:dyld: Library not loaded
- spring,mybatis事务管理配置与@Transactional注解使用
- 定位方法,以及纠偏的调用
- JSON.stringify 语法实例讲解
- iOS开发-玩玩渐变导航栏
- 关于Activity的启动过程
- 水晶报表VS2008使用
- scrollview拦截子控件的滑动
- CentOS使用EPEL YUM源教程
- QEMU 使用的镜像文件:qcow2 与 raw
- 图像配准中的变换操作
- 【Xibo】Xibo汉化指南
- Service的启动(startService)