导航栏渐隐透明

作者: 没有名字就是我的名字 | 来源:发表于2017-02-08 11:06 被阅读61次
有很多项目中用了导航栏项目渐隐的效果,在滑动页面的时候显示导航栏######
  • 系统的导航栏是不能满足这个需求的(目前笔者不知道系统的导航栏可以完成这个需求)

  • 我是通过自定义的View来代替系统的导航栏去完成这个需求

  • 废话不多说现在开始撸代码

1. 如果你给当前的控制器加了导航栏之后你需要把系统的给隐藏掉
- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    self.navigationController.delegate = self;
}

2. 懒加载创建一个tableView
#pragma mark -- /*init*/
- (UITableView *)tableView {

        if (!_tableView) {
    
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = self.headerView;
        _tableView.tableFooterView = self.footerView;
    }
    return _tableView;
}
- (UIView *)navigationView {

    if (!_navigationView) {
    
        _navigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
        _navigationView.backgroundColor = [UIColor whiteColor];
        _navigationView.alpha = 0.0;
        [_navigationView addSubview:self.titleLabel];
        [_navigationView addSubview:self.backButton];
    }
    return _navigationView;
}
- (UIView *)headerView {

    if (!_headerView) {
    
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
        [_headerView addSubview:self.headerImageView];
    }
    return _headerView;
}
- (UIImageView *)headerImageView {
    
    if (!_headerImageView) {
    
        _headerImageView = [[UIImageView alloc] initWithFrame:self.headerView.bounds];
        _headerImageView.image = [UIImage imageNamed:@"1234"];
    }
    return _headerImageView;
  }
- (UILabel *)titleLabel {

    if (!_titleLabel) {
    
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 64)];
        _titleLabel.center = self.navigationView.center;
        _titleLabel.text = @"西天取经";
        _titleLabel.alpha = 0.0;
    }
        return _titleLabel;
}
- (UIButton *)backButton {

    if (!_backButton) {
    
        _backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 64)];
        [_backButton setImage:[UIImage imageNamed:@"cancel"] forState:UIControlStateNormal];
        _backButton.alpha = 0.0;
    }
    return _backButton;
}
3. 把你的tableView和自定义的View以及其他控件都add进去

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.view addSubview:self.tableView];

    [self.view addSubview:self.navigationView];

}
4. 接下来就是tableView的代理和数据源方法
#pragma mark -- /*tableViewData&Delegate*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

     return 15;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (cell == nil) {
    
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    return cell;
}

这个地方是最重要的地方通过监听滚动的位置去判断是否显示导航栏通过yOffset的大小去实时改变导航栏的alpha值从而实现渐隐效果

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    CGFloat yOffset = scrollView.contentOffset.y;

    if (yOffset > 0) {

        self.navigationView.alpha = yOffset / 64;
        self.titleLabel.alpha = yOffset / 64;
        self.backButton.alpha = yOffset / 64;
    
    }else if (yOffset <= 0) {

        self.navigationView.alpha = 0.0;
        self.titleLabel.alpha = 0.0;
        self.backButton.alpha = 0.0;
    }

}

最后附上定义的几个属性

@property(nonatomic,strong)UITableView *tableView;/**< 界面布局*/

@property(nonatomic,strong)UIView *navigationView;/**< 渐变的nav*/
@property(nonatomic,strong)UIView *headerView;/**< 头*/
@property(nonatomic,strong)UIImageView *headerImageView;/**< 图片*/

@property(nonatomic,strong)UILabel *titleLabel;/**< 标题*/

@property(nonatomic,strong)UIButton *backButton;/**< 返回按钮*/

不要忘记遵循tableView的代理和数据源哦~

相关文章

  • 导航栏渐隐透明

    有很多项目中用了导航栏项目渐隐的效果,在滑动页面的时候显示导航栏###### 系统的导航栏是不能满足这个需求的(目...

  • Android状态栏和导航栏透明

    透明状态栏+透明导航栏 仅透明导航栏

  • 导航栏渐变隐现

    页面不显示导航栏,上托一定的距离显示导航栏. 设置导航栏存在且透明: 1.设置导航栏的透明: //导航栏透明 ...

  • Swift - NavigationBar Tips

    设置导航栏背景色为透明 隐藏导航栏下方的线条 设置导航栏的背景色 设置导航栏背景不为半透明

  • UIViewController 导航栏

    1、导航栏透明显示 2、导航栏不透明显示

  • 迷之“导航栏”

    对于iOS 开发者来说,导航栏确实是一个让人困惑的知识点。比如设置导航栏透明效果,透明导航栏与非透明导航栏之间的跳...

  • iOS 设置导航栏半透明后添加视图变化

    1.设置导航栏为不透明 2.设置导航栏为半透明 [系统默认就是半透明的]注意:iOS 导航栏如果设置为半透明,则其...

  • iOS日志-设置导航栏背景及字体颜色

    返回item颜色 导航栏背景 导航栏透明度 不加会有横线 修改字体颜色及字体大小 设置导航栏透明

  • iOS 导航栏、状态栏

    一、修改导航栏及状态栏的透明度 iOS 修改导航栏及状态栏的透明度 二、导航栏返回按钮靠右问题

  • iO 关于NavigationBar、NavigationIte

    前言:经常遇到这样的需求:从有导航栏的界面跳转到导航栏透明的界面,由于iOS从有导航栏跳转到透明导航栏界面,并且设...

网友评论

    本文标题:导航栏渐隐透明

    本文链接:https://www.haomeiwen.com/subject/ehevittx.html