ios隐藏导航栏底线条和导航、状态栏浙变色

方法一遍历法:函数

在你须要隐藏的地方调用以下代码atom

    [self findlineviw:self.navigationBar].hidden = YES;it

-(UIImageView*)findlineviw:(UIView*)view{io

    

    if ([view isKindOfClass:[UIImageView class]]&&view.bounds.size.height<=1.0) {class

        return (UIImageView*) view;扩展

    }for (UIImageView *subview in view.subviews) {遍历

        UIImageView *lineview = [self findlineviw:subview];scroll

        if (lineview) {方法

            return lineview;im

        }

    }

    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;

}

 

 

3、滚动时改变状态栏的颜色:

-(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]; }}

相关文章
相关标签/搜索