iOS 导航栏渐变

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

if (scrollView==self.ActivityTableview) {

//        NSLog(@"%f",scrollView.contentOffset.y);

CGFloat offsetY = scrollView.contentOffset.y;

if (scrollView.contentOffset.y>=-20 && scrollView.contentOffset.y<50) {

[self.navigationController.navigationBar setBackgroundColor:[UIColor colorWithRed:255/255.0f green:255/255.0f  blue:255/255.0f  alpha:offsetY*2/100]];

[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];

[self setStatusBarBackgroundColor:[UIColor colorWithRed:255/255.0f green:255/255.0f  blue:255/255.0f  alpha:offsetY*2/100]];

//NSLog(@"变色");

}else if (scrollView.contentOffset.y<-20){

[self.navigationController.navigationBar setBackgroundColor:[UIColor colorWithRed:226/255.0f green:67/255.0f  blue:70/255.0f  alpha:0.0]];

[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];

[self setStatusBarBackgroundColor:[UIColor colorWithRed:226/255.0f green:67/255.0f  blue:70/255.0f  alpha:0.0]];

}else{

[self.navigationController.navigationBar setBackgroundColor:[UIColor whiteColor]];

//            [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];

[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], NSForegroundColorAttributeName, nil]];

[self setStatusBarBackgroundColor:[UIColor whiteColor]];

}

}

}

- (void)setStatusBarBackgroundColor:(UIColor *)color {

UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];

if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {

statusBar.backgroundColor = color;

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 原文 在这里总结一些iOS开发中的小技巧,能大大方便我们的开发,持续更新。 1.UITableView的Group...
    无沣阅读 4,127评论 0 2
  • 工作中的一些代码记录 导航栏颜色渐变示例: 导航栏顶部隐藏和显示: 导航栏上拉、下拉 隐藏和显示: navigat...
    郑州程序员王一阅读 4,873评论 0 3
  • 最近在做项目,UI要求做一个导航栏随着ScrollView滚动而渐变的效果,所以开始研究怎么最简洁的方法来实现这个...
    CoderJmd阅读 9,073评论 4 49
  • 东野圭吾的推理能力很强,读完总觉得和《白夜行》似曾相识,文中的雪惠为了得到自己想要的,心狠手辣,对她不利的人都得死...
    成长中的木木阅读 26,265评论 0 7
  • 微信上看到一篇文章《恩爱是可以遗传的》,如果,恩爱是可以遗传的,那该多好呀。 我爸妈出生于1950年,两人于197...
    花与熙阅读 1,612评论 0 1