核心是利用UITextField的secureTextEntry属性隐藏内容,注意该功能仅iOS13.2及以上支持。
UITextField在开启密码模式后,在截屏录屏时隐藏一个子视图。
注:模拟器左上角的截图不支持UITextField的密码隐藏。如果要模拟真机截图,可以点击模拟器菜单 Device->Trigger Screenshot。
所以可以将需要禁止截屏的视图添加到UITextField上,当用户截图时,这个子视图就会被隐藏。 测试代码如下:
// ViewController1.m
UIView *theView = self.textField.subviews.firstObject;
[self.view addSubview:theView];
theView.userInteractionEnabled = YES;
[theView addSubview:self.imageView];
[theView ma_makeConstraints:^(MAAutoLayout * _Nonnull make) {
make.top.equalTo(self.ma_safeAreaTopLayoutGuide);
make.leftRight.equalTo(self.view);
make.width.equalTo(theView.ma_height);
}];
[self.imageView ma_makeConstraints:^(MAAutoLayout * _Nonnull make) {
make.edge.equalTo(theView);
}];
上面的代码运行后,会出现一下效果,真机截图时会将图片隐藏。
一般在项目中会要求隐藏整个界面,为了避免每个界面都需要添加UITextField相关的代码,可以在父类的UIViewController添加如下代码:
// BaseViewController.h
@interface BaseViewController : UIViewController
@property (nonatomic, assign) BOOL banScreenshot;
@end
// BaseViewController.m
@implementation BaseViewController
- (void)loadView {
if (self.banScreenshot) {
UITextField *textField = [[UITextField alloc] init];
textField.secureTextEntry = YES;
textField.enabled = NO;
if (textField.subviews.firstObject != nil) {
self.view = textField.subviews.firstObject;
}else{
self.view = [[UIView alloc] init];
}
}else{
self.view = [[UIView alloc] init];
}
self.view.userInteractionEnabled = YES;
self.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
self.view.backgroundColor = [UIColor whiteColor];
}
@end
只要在子控制器设置self.banScreenshot = YES;
,该界面在用户截图时,就只能截图一张黑色的图片。如果感觉黑色的图片不好看,可以使用Demo中的方式自定义一个MAScreenShieldView
设置self.view
,这样就可以截图成白色的图片了。
除了可以让用户只能截屏一张白色图片外,也可以自定义截图的内容。实现方式:在UITextField的子视图的下面添加自定义截图的内容,在用户截图时,UITextField的子视图上的View会被隐藏,子视图下面的内容会显示出来。
比如添加一个UIlabel,代码如下:
@interface ViewController2 ()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UILabel *label;
@end
@implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.label];
[self.label ma_makeConstraints:^(MAAutoLayout * _Nonnull make) {
make.center.equalTo(self.view);
}];
UIView *theView = self.textField.subviews.firstObject;
[self.view addSubview:theView];
theView.userInteractionEnabled = YES;
[theView addSubview:self.imageView];
[theView ma_makeConstraints:^(MAAutoLayout * _Nonnull make) {
make.edge.equalTo(self.view);
}];
[self.imageView ma_makeConstraints:^(MAAutoLayout * _Nonnull make) {
make.center.equalTo(theView);
}];
}
- (UIImageView *)imageView {
if (!_imageView) {
_imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"123.jpg"]];
_imageView.userInteractionEnabled = YES;
}
return _imageView;
}
- (UITextField *)textField {
if (!_textField) {
_textField = [[UITextField alloc] init];
_textField.secureTextEntry = YES;
_textField.enabled = NO;
}
return _textField;
}
- (UILabel *)label{
if(!_label){
_label = [[UILabel alloc]init];
_label.text = @"禁止截图";
}
return _label;
}
@end
在项目中如果需要自定义截图,可以改造Demo中的
MAScreenShieldView
,在MAScreenShieldView
上面添加你想要的效果。