首先截屏是操作系統(tǒng)級別的操作虚吟,app上的代碼無法靈活到某個頁面的禁止御蒲,可以配置整個app禁止截屏的操作掘剪,但是不符合需求。所以只能退而求其次画拾,改變截屏出來的圖片顯示自己想顯示的內(nèi)容啥繁。
此處屏蔽截屏內(nèi)容的原理如下
利用textField在secureTextEntry=YES截屏時,其內(nèi)容被隱藏的特性青抛,將隱藏的內(nèi)容加到其上面截屏時不顯示旗闽,但是此方法只能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];
}
之前發(fā)現(xiàn)一個問題就是textfeild響應了子view的輸入事件蜜另,導致頁面錯亂問題适室,設置textFieldShouldBeginEditing返回值為NO,使得當前的textField不能編輯蚕钦,若是使用textField.enable=NO亭病,則會影響子view的交互。
- (void)setupUI {
//此處textField在secureTextEntry=YES截屏時嘶居,其內(nèi)容被隱藏的特性罪帖,使加到其上面的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傳送陣在此整袁。