iOS navigationBar和tabBar變透明 & navigationBar根據滑動距離的漸變色實現


    navigationBar變為純透明

    //第一種方法

    //導航欄純透明

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

    //去掉導航欄底部的黑線

    self.navigationBar.shadowImage = [UIImage new];

    

    //第二種方法

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

 

 

    tabBar同理

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

    self.tabBar.shadowImage = [UIImage new];

 

    navigationBar根據滑動距離的漸變色實現

    //第一種

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView

    {

        CGFloat offsetToShow = 200.0;//滑動多少就完全顯示

        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;

    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM