常用组件ios积累自定义控件

自定义导航栏之导航栏颜色随滑动改变

2016-01-22  本文已影响885人  SPIREJ

1- 顶部是一张图片,下面是tableView,图片加载在tableView上是tableViewtableHeaderView
2- 上滑时,逐渐的显示NavigationBar

下面来简单梳理一下:
SJNavigationAlphaController *navAlphaController = [[SJNavigationAlphaController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:navAlphaController];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
 - (void)setUpNavigationBar
{
    self.edgesForExtendedLayout = UIRectEdgeTop;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.navigationController.navigationBar setTranslucent:YES];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationController.view.backgroundColor = [UIColor clearColor];
}
 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    UIColor * color = kNavigationBarColor;
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY < -20 ) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }else{
        [self.navigationController setNavigationBarHidden:NO animated:YES];
        if (offsetY > 50) {
            CGFloat alpha = MIN(1, 1 - ((50 + 64 - offsetY) / 64));
            _titleLabel.textColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:alpha];
            [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:alpha]];
            [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        } else {
            [self setUpNavigationBar];
            _titleLabel.textColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:0];
            [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:0]];
            [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        }
    }
}
示例代码https://github.com/SPIREJ/SJNavgationBarAlpha
上一篇 下一篇

猜你喜欢

热点阅读