您当前的位置: 首页 >  ar

培根芝士

暂无认证

  • 0浏览

    0关注

    446博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

修改UINavigationBar的背景色

培根芝士 发布时间:2015-04-20 14:25:52 ,浏览量:0

首先定义Category

@interface UINavigationBar (BackgroundColor)

- (void)setAlphaBackgroundColor:(UIColor *)color;

@end

#import "UINavigationBar+Alpha.h"

#import

static void *overlayKey = nil;

@implementation UINavigationBar (BackgroundColor)

- (UIView *)overlay {

    return objc_getAssociatedObject(self, overlayKey);

}

- (void)setOverlay:(UIView *)overlay {

    objc_setAssociatedObject(self, overlayKey, overlay, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

- (void)setAlphaBackgroundColor:(UIColor *)backgroundColor {

    if (!self.overlay) {

        [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

        [self setShadowImage:[UIImage new]];        // insert an overlay into the view hierarchy

        self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, -20, [UIScreen mainScreen].bounds.size.width, 64)];

        [self insertSubview:self.overlay atIndex:0];

    }

    self.overlay.backgroundColor = backgroundColor;

}

@end

最后在scrollViewDidScroll中,就可以动态地修改UINavigationBar的backgroundColor了:

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

    UIColor *color = [UIColor blueColor];

    CGFloat offsetY = scrollView.contentOffset.y;

    if (offsetY > 0) {

        CGFloat alpha = 1 - ((64 - offsetY) / 64);

        NSLog(@"alpha:%f", alpha);

        [self.navigationController.navigationBar setAlphaBackgroundColor:[color colorWithAlphaComponent:alpha]];

    }

    else {

        [self.navigationController.navigationBar setAlphaBackgroundColor:[color colorWithAlphaComponent:0]];

    }

}

关注
打赏
1660824269
查看更多评论
立即登录/注册

微信扫码登录

0.0367s