iOS开发-玩玩渐变导航栏

最近博主对渐变导航栏进行了改进,原来的是只有在头部部分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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodingFire

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值