设置导航栏透明度

本文详细介绍了如何在iOS应用中实现导航栏透明度的动态调整,包括在不同场景下设置不同透明度的方法,以及如何在滚动视图时平滑过渡的效果。通过自定义导航栏和监听滚动事件,你可以为用户创建更沉浸式的交互体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self drawNavi];
    [self drawMainView];
    //设置右键大小
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:13],NSFontAttributeName, nil] forState:UIControlStateNormal];
}

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    //左侧建
    [self setBackLeftBtn];

    //去掉背景色和下面的线
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new]  forBarMetrics:UIBarMetricsDefault];

    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    //初始化导航栏颜色和左右键颜色
    [self setNavigationBarChangeWhite:YES];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
     //去掉导航栏设置的左键
    [self.leftBtn removeFromSuperview];

    //还原设定
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
    [self setNavigationBarChangeWhite:NO];
}


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

    //先获取y轴偏移
    CGFloat yOffset  = scrollView.contentOffset.y;

    //设置透明度
    CGFloat alpha = fabs(yOffset)/kTopHeight;

    if (alpha<0.5) {
        [self setNavigationBarChangeWhite:YES];
    } else {
        [self setNavigationBarChangeWhite:NO];
    }

    //设置导航栏的背景图片(将颜色生成的图片)
    //imageWithColor为扩展的方法根据颜色生成图片
    [self.navigationController.navigationBar setBackgroundImage:[UIColor imageWithColor:[[UIColor whiteColor]colorWithAlphaComponent:alpha]] forBarMetrics:UIBarMetricsDefault];

}



-(void)setNavigationBarChangeWhite:(BOOL)isChange{

    if(isChange){
        //设置自定义的左键颜色
        self.leftBtn.nameL.textColor = k_white_color;

        //设置右键的颜色(复用导航栏的左右键时候)
        [self.navigationController.navigationBar setTintColor:k_white_color];

        //设置导航栏title的颜色
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:k_white_color,NSFontAttributeName:TEXT_MIDD_B_FONT16}];

    }else{
        //设置自定义的左键颜色
        self.leftBtn.nameL.textColor = k_title_color;

        //设置导航栏title的颜色
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:k_title_color,NSFontAttributeName:TEXT_MIDD_B_FONT16}];

        //设置右键的颜色(复用导航栏的左右键时候)
        [self.navigationController.navigationBar setTintColor:k_title_color];

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值