1.项目需求,进入之后导航栏透明,header是一张图片,完全展示出来,随着页面向上滑动,导航栏透明度逐渐减小,当滑动的位置偏移超过固定值时,导航栏出现.
代码实现:
1.
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self requestData];
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//去掉导航条线面的黑线
[self.navigationBar setShadowImage:[UIImage new]];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setLeftButtonWithImage:@"icon_back_white" highlightedImage:nil target:self action:@selector(onBack)];
[self setRightButtonWithImage:@"icon_share_white" highlightedImage:nil target:self action:@selector(clickShare)];
CGRect frame = self.navigationController.navigationBar.frame;
self.alphaView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, frame.size.width, frame.size.height+20)];
self.alphaView.backgroundColor = [UIColor whiteColor];
self.alphaView.alpha = 0.0;
self.alphaView.userInteractionEnabled = NO;
[self.navigationController.navigationBar insertSubview: self.alphaView atIndex:0];
self.scrollView.backgroundColor = [UIColor colorWithHexString:@"F0F0F0"];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
UIImage *backImage = [[UIImage imageWithColor:[UIColor whiteColor]] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeTile];
[self.navigationBar setBackgroundImage:backImage forBarMetrics:UIBarMetricsDefault];
[self.navigationBar setShadowImage:nil];
}
2.出现问题:
iPhone效果正常;在iPad Air2 系统版本12.2模拟器上好使,在iPad Air2 系统版本:11.0.3上失效.
真机图层显示:
模拟器显示:
3:修改;
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self requestData];
if (DEVICE_IS_IPAD) {
UIImage *backImage = [[UIImage imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:(_offset / 64) >0.99 ?0.99 :(_offset / 64)]] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeTile];
[self.navigationController.navigationBar setBackgroundImage:backImage forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
} else {
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//去掉导航条线面的黑线
[self.navigationBar setShadowImage:[UIImage new]];
}
}