iOS如何将工程内所有页面都加上全屏水印(工程里页面很多忘记哪个页面叫什么ViewController怎么办)

 

 

 

分两步:

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]) 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值