美文网首页
runtime在viewWillAppear获取当前控制器的类名

runtime在viewWillAppear获取当前控制器的类名

作者: 冰点雨 | 来源:发表于2022-11-08 09:20 被阅读0次

    UIViewController+Utils.h

    NS_ASSUME_NONNULL_BEGIN
    
    @interface UIViewController (Utils)
    /**
    Whether or not to set ModelPresentationStyle automatically for instance, Default is [Class LL_automaticallySetModalPresentationStyle].
    
    @return BOOL
    */
    @property (nonatomic, assign) BOOL LL_automaticallySetModalPresentationStyle;
    
    /**
     Whether or not to set ModelPresentationStyle automatically, Default is YES, but UIImagePickerController/UIAlertController is NO.
    
     @return BOOL
     */
    + (BOOL)LL_automaticallySetModalPresentationStyle;
    @end
    
    NS_ASSUME_NONNULL_END
    

    UIViewController+Utils.m

    #import "UIViewController+Utils.h"
    #import <objc/runtime.h>
    
    static const char *LL_automaticallySetModalPresentationStyleKey;
    @implementation UIViewController (Utils)
    + (void)load {
        Method originAddObserverMethod = class_getInstanceMethod(self, @selector(presentViewController:animated:completion:));
        Method swizzledAddObserverMethod = class_getInstanceMethod(self, @selector(LL_presentViewController:animated:completion:));
        method_exchangeImplementations(originAddObserverMethod, swizzledAddObserverMethod);
        method_exchangeImplementations(class_getInstanceMethod(self, @selector(viewWillAppear:)), class_getInstanceMethod(self, @selector(iv_viewWillAppear:)));
    }
    -(void)iv_viewWillAppear:(BOOL)animated
    {
        [self iv_viewWillAppear:animated];
        NSLog(@"😊😊😊😊😊😊\n\n\n | %@ \n\n\n 😊😊😊",self.class);
    }
    - (void)setLL_automaticallySetModalPresentationStyle:(BOOL)LL_automaticallySetModalPresentationStyle {
        objc_setAssociatedObject(self, LL_automaticallySetModalPresentationStyleKey, @(LL_automaticallySetModalPresentationStyle), OBJC_ASSOCIATION_ASSIGN);
    }
    
    - (BOOL)LL_automaticallySetModalPresentationStyle {
        id obj = objc_getAssociatedObject(self, LL_automaticallySetModalPresentationStyleKey);
        if (obj) {
            return [obj boolValue];
        }
        return [self.class LL_automaticallySetModalPresentationStyle];
    }
    
    + (BOOL)LL_automaticallySetModalPresentationStyle {
        if ([self isKindOfClass:[UIImagePickerController class]] || [self isKindOfClass:[UIAlertController class]]) {
            return NO;
        }
        return YES;
    }
    
    - (void)LL_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
        if (@available(iOS 13.0, *)) {
            if (viewControllerToPresent.LL_automaticallySetModalPresentationStyle) {
                viewControllerToPresent.modalPresentationStyle = UIModalPresentationFullScreen;
            }
            [self LL_presentViewController:viewControllerToPresent animated:flag completion:completion];
        } else {
            // Fallback on earlier versions
            [self LL_presentViewController:viewControllerToPresent animated:flag completion:completion];
        }
    }
    @end
    
    

    相关文章

      网友评论

          本文标题:runtime在viewWillAppear获取当前控制器的类名

          本文链接:https://www.haomeiwen.com/subject/ogkwtdtx.html