首先截屏是操作系统级别的操作,app上的代码无法灵活到某个页面的禁止,可以配置整个app禁止截屏的操作,但是不符合需求。所以只能退而求其次,改变截屏出来的图片显示自己想显示的内容。
此处屏蔽截屏内容的原理如下
利用textField在secureTextEntry=YES截屏时,其内容被隐藏的特性,将隐藏的内容加到其上面截屏时不显示,但是此方法只能iOS13以上使用。
- (void)viewDidLoad {
[super viewDidLoad];
if (@available(iOS 13.2, *)) {
GDForbidScreenShotView *tempView = [[GDForbidScreenShotView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
[tempView addSubview:self.view];
self.view = tempView;
}
self.view.backgroundColor = UIColor.whiteColor;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(150, 80, 100, 40)];
button.backgroundColor = UIColor.blueColor;
[button setTitleColor:UIColor.whiteColor forState:(UIControlStateNormal)];
button.titleLabel.font = [UIFont systemFontOfSize:15];
[button setTitle:@"这是个按钮" forState:(UIControlStateNormal)];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
}
之前发现一个问题就是textfeild响应了子view的输入事件,导致页面错乱问题,设置textFieldShouldBeginEditing返回值为NO,使得当前的textField不能编辑,若是使用textField.enable=NO,则会影响子view的交互。
- (void)setupUI {
//此处textField在secureTextEntry=YES截屏时,其内容被隐藏的特性,使加到其上面的view都截屏上不显示
[self addSubview:self.textField];
self.textField.subviews.firstObject.userInteractionEnabled = YES;
[self.textField.subviews.firstObject addSubview:self.clearView];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
- (void)addSubview:(UIView *)view {
[super addSubview:view];
if (self.textField != view) {
[self.clearView addSubview:view];
}
}
- (void)keyboardWillShow:(NSNotification *)noti {
if (self.textField.isFirstResponder) {
[self.textField resignFirstResponder];
self.textField.subviews.firstObject.userInteractionEnabled = YES;
}
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO;
}
在此记录一下,看到的希望能帮到你。本篇GitHubDemo传送阵在此。