分两步:
1.如何生成水印 参考https://www.jianshu.com/p/fd1800545513
2.水印add到哪个view上
水印add到哪个view上,尝试了三种:
第一种:hook uiview 的addsubview方法 ,使其全部透明,把tableview的layer 改为水印。
缺点:改造范围大,某些view无法过滤,有些不生效因为滑动时会layoutSubviews重新布局,某些白色背景圆角都会变成透明,不太好
第二种:UIViewController分类重写viewDidLayoutSubviews方法,水印加在self.view上,对原生页面分别处理。比如首页,登录页,但h5页无法添加。也需要重写UITableViewCell的layoutSubviews遍历子view变透明。
第三种(最后使用的):直接加到self.view上,只要透明度足够,就看不出来是加在上面的。
当然要排除导航类,容器类的,系统私有的控制器
在UIViewController的分类中写
.h
@interface UIViewController (YYY)
@end
.m
@implementation UIViewController (YYY)
+ (void)load{
{
Method originalMethod = class_getInstanceMethod([NSClassFromString(@"UIViewController") class], @selector(viewDidLayoutSubviews));
Method swizzledMethod = class_getInstanceMethod([self class], @selector(viewDidLayoutSubviewsS));
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
- (void)viewDidLayoutSubviewsS{
NSArray *classES = @[
@"UITabBarController",
@"UINavigationController",
@"_UIRemoteInputViewController",
@"PUPhotoPickerHostViewController",
@"UIApplicationRotationFollowingController",
@"UISystemInputAssistantViewController",
@"UIInputWindowController",
@"UISystemKeyboardDockController",
@"UICompatibilityInputViewController",
@"UIApplicationRotationFollowingController",
@"UIAlertController",
@"UIEditingOverlayViewController",
@"UIPredictionViewController",
@"_UIWaitingForRemoteViewContainerViewController",
@"_UIResilientRemoteViewContainerViewController",
@"CAMPreviewViewController",
@"PLPhotoTileViewController",
@"CAMViewfinderViewController",
@"UIDocumentPickerViewController",
@"UIDocumentBrowserViewController",
@"CAMImagePickerCameraViewController",
@"_UIContextMenuActionsOnlyViewController",
@"UIImagePickerController",
@"DOCRemoteViewController"
];
if([classES containsObject:NSStringFromClass([self class])]
){
}else{
UIView *markView = [[UIView alloc] initWithFrame:self.view.frame];
markView.tag = 666;
AddWaterMark *mark = [[AddWaterMark alloc] init];
mark.textFont = [UIFont systemFontOfSize:14];
mark.textColor = [UIColor lightGrayColor];
UIImage *logo = [mark watemarkText: @"666" ];
markView.layer.contents = (__bridge id _Nullable)(logo.CGImage);
markView.userInteractionEnabled = NO;
markView.alpha = 0.3;
if ([self.view viewWithTag:666]) {
}else{
[self.view addSubview:markView];
}
}
}
工程里页面很多忘记哪个页面叫什么ViewController怎么办?把上面text换成NSStringFromClass([self class])