导航栏上拉渐变,,头视图下拉放大

本文介绍如何在iOS应用中自定义状态栏样式及导航栏颜色,并实现随滚动视图变化的效果。通过修改info.plist文件配置和利用Objective-C代码,实现了状态栏样式切换、导航栏渐变显示及按钮颜色变化等功能。

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




 /// 状态栏变色  info.plist 添加 View controller-based status bar appearance  为 NO

- (UIStatusBarStyle)preferredStatusBarStyle{

    if (self.changeColor == YES) {

        return UIStatusBarStyleLightContent;

    }else{

        return UIStatusBarStyleDefault;

    }

}


1.

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    [weakSelf.navigationController setNavigationBarHidden:YES animated:YES];

}

-(void)viewWillDisappear:(BOOL)animated{

    [super viewWillAppear:animated];

    [self.navigationController setNavigationBarHidden:NO animated:YES];

}


2.

- (void)viewDidLoad {

    [super viewDidLoad];

    // 导航条

    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Width, nanH)];

    self.headView.alpha = 0;

    self.headView.backgroundColor = [UIColor whiteColor];

    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, nanH - 0.7, Width, 0.7)];

    [self.headView addSubview:line];

    line.backgroundColor = [UIColor lightGrayColor];

    [self.view addSubview:self.headView];

    ///

    self.backImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"返回-7"]];

    self.backImage.frame = CGRectMake(18, statusH+12, 20, 20);

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backAction)];

    self.backImage.userInteractionEnabled = YES;

    [self.backImage addGestureRecognizer:tap];

    [self.view addSubview:self.backImage];

    ///

    self.backButton = [UIButton buttonWithType:UIButtonTypeSystem];

 [self.backButton setTitle:NSLocalizedString(@"Back", nil) forState:UIControlStateNormal];

    [self.view addSubview:self.backButton];

    self.backButton.tintColor = [UIColor whiteColor];

    self.backButton.frame = CGRectMake(35, statusH+12, 40, 20);

    [self.backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];

    //

    self.titlLabel = [[UILabel alloc] initWithFrame:CGRectMake(Width/2-100, statusH+12, 200, 20)];

    self.titlLabel.text = NSLocalizedString(@"Personal circle", nil);

    [self.view addSubview:self.titlLabel];

    self.titlLabel.textAlignment = NSTextAlignmentCenter;

    self.titlLabel.textColor = [UIColor whiteColor];

    //// 刷新状态栏的颜色

    self.changeColor = YES;

    [self setNeedsStatusBarAppearanceUpdate];

    /// 创建tableview  添加头视图

}


3.


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

CGFloat scroll=scrollView.contentOffset.y;

    CGFloat imageH = 209;///209头视图高度

    /// 导航栏View渐变

    if (scroll > 0) {

        self.headView.alpha = 1 * (scroll / (imageH-64));

    }else{

        self.headView.alpha = 0;

    }

    /// 导航栏返回按钮标题变色

    if (scroll>=(imageH-64-49)) {

        self.backImage.image = [UIImage imageNamed:@"返回-4"];

        self.backButton.tintColor = [UIColor blackColor];

        self.titlLabel.textColor = [UIColor blackColor];

        //// 刷新状态栏的颜色

        self.changeColor = NO;

        [self setNeedsStatusBarAppearanceUpdate];

    }else{

        self.backImage.image = [UIImage imageNamed:@"返回-7"];

        self.backButton.tintColor = [UIColor whiteColor];

        self.titlLabel.textColor = [UIColor whiteColor];

        //// 刷新状态栏的颜色

        self.changeColor = YES;

        [self setNeedsStatusBarAppearanceUpdate];

    }

    /// 头图缩放

    if (scroll<0) {

        self.headerView.backImageView.frame = CGRectMake(scroll*Width/imageH/2, scroll, Width+2*fabs(scroll*Width/imageH), imageH+fabs(scroll));

    }else{

        self.headerView.backImageView.frame = CGRectMake(0, 0, Width, imageH);

    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值