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