方法一遍历法:
在你需要隐藏的地方调用如下代码
[self findlineviw:self.navigationBar].hidden = YES;
-(UIImageView*)findlineviw:(UIView*)view{
if ([view isKindOfClass:[UIImageView class]]&&view.bounds.size.height<=1.0) {
return (UIImageView*) view;
}for (UIImageView *subview in view.subviews) {
UIImageView *lineview = [self findlineviw:subview];
if (lineview) {
return lineview;
}
}
return nil;
}
方法二:注意这两句代码一句需要同时配合使用才有效果,可以在不同函数调用,但是必须保证两句代码都会执行到。
self.navigationBar.shadowImage = [UIImage new];
[self.navigationBar setBackgroundImage:[UIImage imageWithColor:kClearColor] forBarMetrics:UIBarMetricsDefault];
二,设置导航栏浙变
方法一:
定制一个view添加到导航栏上:
- (UIView *)navBarView {
if (!_navBarView) {
UIView *navBarView = [[UIView alloc] init];
navBarView.frame = CGRectMake(0, -20, kScreenWidth, 64);
[self.navigationController.navigationBar addSubview:navBarView];
self.navBarView = navBarView;
}
return _navBarView;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
self.navigationItem.title = @"";
CGFloat offsetY = scrollView.contentOffset.y;
if (offsetY > startH) {
CGFloat alpha = MIN(1, 1 - ((startH + 64 - offsetY) / 64));
NSLog(@"%f",alpha);
self.navBarView.backgroundColor = [kBlackColor colorWithAlphaComponent:alpha];
if (offsetY >= (startH + 64)){
//这里设置滚动的时候是否显示标题,不需要可以注释掉
self.navigationItem.title = @"主页";
}
} else {
self.navBarView.backgroundColor = kClearColor;
}
}
方法二:直接改变导航栏的颜色
申明:
/** 滚动到多少高度开始出现 */
static CGFloat const startH = 0;
/** 导航条View */
@property (nonatomic, weak) UIView *navBarView;
实现:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//设置导航栏的初始颜色
// [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
//建议用这个方法设置初始化颜色,如果不设置barTintColor的颜色,那setBackgroundImage为clearColor时导航栏底色会成默认色
self.navigationController.navigationBar.barTintColor=[UIColor orangeColor];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//这里设置滚动的时候是否显示标题,不需要可以注释掉
self.navigationItem.title = @"";
CGFloat offsetY = scrollView.contentOffset.y;
if (offsetY > startH) {
CGFloat alpha = MIN(1, 1 - ((startH + 64 - offsetY) / 64));
NSLog(@"%f",alpha);
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[kBlackColor colorWithAlphaComponent:alpha]] forBarMetrics:UIBarMetricsDefault];
if (offsetY >= (startH + 64)){
//这里设置滚动的时候是否显示标题,不需要可以注释掉
self.navigationItem.title = @"主页";
}
} else {
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:kClearColor] forBarMetrics:UIBarMetricsDefault];
}
}
方法三:导航栏浙变
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat offset = scrollView.contentOffset.y;
if (offset<=0&&offset<=-90) {
self.navigationController.navigationBar.alpha = 0;
}else if(offset<=500){
self.navigationController.navigationBar.alpha = offset/200;
}
}
类扩展的方法:
+ (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 *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
三、滚动时改变状态栏的颜色:
-(void)viewWillAppear:(BOOL)animated
{ self.navigationController.navigationBarHidden = YES;
UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
statusBarView.backgroundColor = kOrangeColor;
[self.view addSubview:statusBarView];
_statusBarView = statusBarView;
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat offsetY = scrollView.contentOffset.y;
if (offsetY > startH) {
CGFloat alpha = MIN(1, (1 - ((startH + 64 - offsetY) / 64))/10.f);
NSLog(@"%f",alpha);
_statusBarView.backgroundColor = [kBlackColor colorWithAlphaComponent:alpha];
// [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[kBlackColor colorWithAlphaComponent:alpha]] forBarMetrics:UIBarMetricsDefault];
if (offsetY >= (startH + 64)){
_homePageTableView.backgroundColor = kColorHexValue(0xe6ebeb, 1);
}
} else {
_homePageTableView.backgroundColor = kOrangeColor;
_statusBarView.backgroundColor = kOrangeColor;
// [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:kClearColor] forBarMetrics:UIBarMetricsDefault];
}
}