1.隱藏導航的最下面的線。blog
2.設置導航背景的不透明度(ios7.0以上不用設置,translucent默認的就是Yes不用修改,但若是別人修改了或出現那種狀況,須要設置爲Yes)。圖片
3.頁面消失時導航恢復原樣。get
4.滾動時調用方法設置alpha來控制導航背景的漸變(也是核心,重點在這裏)。it
核心代碼以下:io
1.將顏色轉換爲圖片class
- (UIImage *)imageWithColor:(UIColor *)color { //建立1像素區域並開始圖片繪圖 CGRect rect = CGRectMake(0, 0, 1, 1); UIGraphicsBeginImageContext(rect.size); //建立畫板並填充顏色和區域 CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColor]); CGContextFillRect(context, rect); //從畫板上獲取圖片並關閉圖片繪圖 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }
2.滾動時調用改變導航漸變ios7
- (void)changeColor:(UIColor *)color withScrollView:(UIScrollView *)scrollView andValud:(CGFloat)value{ /** if (scrollView.contentOffset.y < 0) { 慎重選擇這個,只適用於固定的導航欄,若是是自定義view效果會不同 //下拉時導航隱藏 self.hidden = YES; }else{ */ self.hidden = NO; //計算透明度 CGFloat alpha = scrollView.contentOffset.y /value > 1.0f ? 1 : scrollView.contentOffset.y/value; //設置顏色改成圖片 UIImage *image = [self imageWithColor:[color colorWithAlphaComponent:alpha]]; [self setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; self.translucent = alpha >=1.0f ? NO : YES; /* }*/ }
3.隱藏導航欄下的線scroll
- (void)start{ UIImageView *shawImage = [self findNavLineImageOn:self]; shawImage.hidden = YES; self.translucent = YES; }
4.頁面消失後重置方法
(適用於導航欄不適用於自定義view) UIImageView *shawImage = [self findNavLineImageOn:self]; shawImage.hidden = NO; [self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault]; self.translucent = NO;
5.參考文章:https://www.jianshu.com/p/10c71cb19b5e