导航栏渐变效果
2018-12-06 本文已影响8人
xieyinghao
-(void)viewWillAppear:(BOOL)animated
{ [super viewWillAppear:animated];
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.YHtableview];
if (_offsetY > 0 && _offsetY < 135) {
CGFloat alpha = _offsetY/135.0;
[self.navigationController setNavigationBarHidden:NO animated:NO];
[[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:alpha];
} else if (_offsetY >= 135) {
[self.navigationController setNavigationBarHidden:NO animated:NO];
[[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:1];
} else {
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:1];
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
#pragma mark-**********懒加载***********
-(UITableView *)YHtableview
{
if (!_YHtableview) {
_YHtableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
_YHtableview.delegate = self;
_YHtableview.dataSource = self;
[_YHtableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
if (@available(iOS 11.0, *)) {
_YHtableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
UIView *TopView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
TopView.backgroundColor = [[UIColor redColor]colorWithAlphaComponent:0.5];
_YHtableview.tableHeaderView = TopView;
}
return _YHtableview;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
return cell;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"+++++%f++++++",scrollView.contentOffset.y);
_offsetY = scrollView.contentOffset.y;
if (_offsetY > 0 && _offsetY < 135) {
CGFloat alpha = _offsetY/135.0;
[self.navigationController setNavigationBarHidden:NO animated:NO];
[[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:alpha];
}
if (_offsetY >= 135) {
[self.navigationController setNavigationBarHidden:NO animated:NO];
[[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:1];
}
if (_offsetY <= 0) {
[self.navigationController setNavigationBarHidden:YES animated:NO];
}}
Simulator Screen Shot - iPhone 7 - 2018-12-06 at 14.14.42.png