调整颜色导航栏- 祛除导航栏白边

本文介绍两种iOS应用中自定义导航栏的方法:一是通过修改导航栏背景颜色和阴影实现渐变效果;二是创建自定义视图替换默认导航栏,并随着滚动视图的变化调整透明度。

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

祛除导航栏白边
self.navigationController.navigationBar.translucent = NO;
[self.navigationController.navigationBar setBackgroundImage: [UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];

  • (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:[self imageWithBgColor:[UIColor colorWithRed:0 green:0 blue:0 alpha: self.newsTableView.contentOffset.y / 200]] forBarMetrics:UIBarMetricsDefault];

    if(scrollView.contentOffset.y > 44){
    self.navigationItem.title = @”发现”;
    }

    if(scrollView.contentOffset.y < 44){
    self.navigationItem.title = @”“;
    }
    }

-(UIImage )imageWithBgColor:(UIColor )color
{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

第二种方式:

pragma AutoShow - NavigationView

  • (void)setupNavigationTitle
    {
    self.navBackView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    [self.view addSubview: self.navBackView];

    UILabel *navigationTitleLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 100, 44)];
    navigationTitleLabel.textAlignment = NSTextAlignmentCenter;
    navigationTitleLabel.textColor = [UIColor whiteColor];
    navigationTitleLabel.text = @”\U0000e612”;
    navigationTitleLabel.font = [UIFont fontWithName: FONT_NAME_ICON size: 19];
    navigationTitleLabel.center = self.navBackView.center;
    self.navigationTitleLabel = navigationTitleLabel;
    [self.navBackView addSubview: self.navigationTitleLabel];
    }

  • (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
    self.navBackView.backgroundColor = [UIColor colorWithPatternImage: [JIHUtils imageFromColor: [UIColor colorWithRed:0 green:0 blue:0 alpha: self.newsTableView.contentOffset.y / 200]]];
    self.navigationTitleLabel.textColor = [UIColor colorWithWhite: 1 alpha: self.newsTableView.contentOffset.y / 200];
    }

  • (void)viewDidDisappear:(BOOL)animated
    {
    [super viewDidDisappear:YES];
    }

  • (void)viewWillAppear:(BOOL)animated
    {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值