问题表现, 如图
在添加过灰色滤镜之后,tableView底部显示成了灰色
代码如下
.h
@interface TPBlackWhiteScreenTool : NSObject
+ (void)addGrayFilterToView:(UIView *)view;
+ (void)removeGrayFilterFromView:(UIView *)view;
@end
@implementation TPBlackWhiteScreenTool
+ (void)addGrayFilterToView:(UIView *)view {
if (@available(iOS 13.0,*)) {
[view.kGrayView removeFromSuperview];
UIView *greyView = [[UIView alloc] initWithFrame:CGRectZero];
greyView.userInteractionEnabled = NO;
greyView.backgroundColor = [UIColor lightGrayColor];
greyView.layer.compositingFilter = @"saturationBlendMode";
greyView.layer.zPosition = FLT_MAX;
[view addSubview:greyView];
[greyView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(UIEdgeInsetsZero);
}];
view.kGrayView = greyView;
}
}
+ (void)removeGrayFilterFromView:(UIView *)view {
UIView *greyView = view.kGrayView;
[greyView removeFromSuperview];
}
@end
调用的地方
- (void)viewDidLoad {
[super viewDidLoad];
[TPBlackWhiteScreenTool addGrayFilterToView:self.view];
后来经过调查发现
tableView的背景色是透明的,控制器的视图背景色也是透明的,cell是有背景色的,但是当我们滑动到底部的时候,cell是被拖到上面去了,这时候滤镜的颜色就会透出来
修复方法
方法1
给tableView 添加背景颜色
self.tableView.backgroundColor = [UIColor colorWithHexString:whiteBackColor];
方法2
给控制器的view 添加背景色
self.view.backgroundColor = [UIColor colorWithHexString:whiteBackColor];
这时候效果就是正常的了
原理**:我们添加滤镜的时候,其实是添加到父视图的下面的,并且滤镜的灰色会向上穿透,直到遇到颜色不透明的视图为止,
所以,我们这里不论是给tableView 设置背景色,或者给view 设置背景色,都会阻止滤镜灰色向上穿透**