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


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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,784评论 2 45
  • 我踏上列车去远方那地方很远他们觉得漫长不需要太远下一站再见 我在列车里感到幸运因为他们被关在外面 我目光散漫找不到...
    小二不2阅读 3,146评论 6 14
  • 明天就是国庆节了,于是今天下午就懒散了许多,去接小女儿回家,带着两个孩子在小区里跑呀跑呀,她们从运动器材处跑到咯吱...
    Lilywang123阅读 1,787评论 0 0
  • 原文链接: Callbacks, RemoteViews and Notifications原文作者: Futur...
    小鄧子阅读 4,987评论 0 4

友情链接更多精彩内容