iOS navigationBar和tabBar變透明 & navigationBar根據(jù)滑動(dòng)距離的漸變色實(shí)現(xiàn)

轉(zhuǎn)載自網(wǎng)友“tongyuling”
原博客地址 http://www.cnblogs.com/tongyuling/p/5822342.html

navigationBar變?yōu)榧兺该?/p>

//第一種方法

//導(dǎo)航欄純透明

[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

//去掉導(dǎo)航欄底部的黑線

self.navigationBar.shadowImage = [UIImage new];



//第二種方法

[[self.navigationBar subviews] objectAtIndex:0].alpha = 0;





tabBar同理

[self.tabBar setBackgroundImage:[UIImage new]];

self.tabBar.shadowImage = [UIImage new];



navigationBar根據(jù)滑動(dòng)距離的漸變色實(shí)現(xiàn)

//第一種

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    CGFloat offsetToShow = 200.0;//滑動(dòng)多少就完全顯示

    CGFloat alpha = 1 - (offsetToShow - scrollView.contentOffset.y) / offsetToShow;

    [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = alpha;

}

//第二種

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    CGFloat offsetToShow = 200.0;

    CGFloat alpha = 1 - (offsetToShow - scrollView.contentOffset.y) / offsetToShow;

    

    [self.navigationController.navigationBar setShadowImage:[UIImage new]];

    [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[[UIColor orangeColor]colorWithAlphaComponent:alpha]] forBarMetrics:UIBarMetricsDefault];

}



//生成一張純色的圖片

- (UIImage *)imageWithColor:(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 *theImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    return theImage;

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,283評(píng)論 25 708
  • 在此特此聲明:一下所有鏈接均來自互聯(lián)網(wǎng),在此記錄下我的查閱學(xué)習(xí)歷程,感謝各位原創(chuàng)作者的無私奉獻(xiàn) ! 技術(shù)一點(diǎn)一點(diǎn)積...
    遠(yuǎn)航的移動(dòng)開發(fā)歷程閱讀 11,214評(píng)論 12 197
  • 大部分的后端會(huì)很很鄙視前端。我也不知道為什么,可能大部分人都會(huì)覺得腳本語言根本不算語言。 大多人 會(huì)叫我們切圖仔,...
    小黑的眼閱讀 3,428評(píng)論 0 15
  • 改良版土豆絲餅 經(jīng)過上次的理論學(xué)習(xí)與實(shí)踐操作,于土豆絲餅這張考卷還算交了一份答案吧,但還是覺...
    yiyi_1650閱讀 297評(píng)論 0 6
  • 鷓鴣天·秋殤 詞/紅塵一凡 冷雨颼颼灑閣窗, 黃昏無語對(duì)簾霜。 階前花落聲漸遠(yuǎn), 帳內(nèi)芳菲欲斷腸。 捎雁信,問斜...
    37度女人_8dda閱讀 526評(píng)論 3 4