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
原创粉丝点击