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

相關文章
相關標籤/搜索