美文网首页
tableview下拉放大头部图片,上滑改变导航栏颜色

tableview下拉放大头部图片,上滑改变导航栏颜色

作者: ios_stand | 来源:发表于2017-06-14 18:05 被阅读0次

- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
{
    CGPoint offset = scrollView.contentOffset;
    if (offset.y < 0) {
        CGRect rect =headViewShow.frame;
        rect.origin.y = offset.y;
        rect.size.height =CGRectGetHeight(rect)-offset.y;
        //注意 backgImgView的contentMode属性为UIViewContentModeScaleAspectFill
        backgImgView.frame = rect;
        backgImgView.clipsToBounds=NO;

    }
    [self.navigationController.navigationBar setBackgroundImage:[self imageWithBgColor:[UIColor colorWithRed:39/255.0 green:44/255.0 blue:48/255.0 alpha:scrollView.contentOffset.y / 60]] forBarMetrics:UIBarMetricsDefault];
//    setBackgroundColor方法 状态栏会出现白色
//    [self.navigationController.navigationBar setBackgroundColor:[UIColor colorWithRed:39/255.0 green:44/255.0 blue:48/255.0 alpha:scrollView.contentOffset.y / 60]];
}

-(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;
    
}

有一个第三库可快速实现导航栏的颜色渐变:https://github.com/ltebean/LTNavigationBar

相关文章

网友评论

      本文标题:tableview下拉放大头部图片,上滑改变导航栏颜色

      本文链接:https://www.haomeiwen.com/subject/hsleqxtx.html