美文网首页
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获取当前控制器的类名

    UIViewController+Utils.h UIViewController+Utils.m

  • PHP获取当前类名、函数名、方法名

    PHP获取当前类名、方法名__CLASS__ 获取当前类名__FUNCTION__ 当前函数名(co...

  • 获取当前类名

    适用于非静态方法:this.getClass().getName()适用于静态方法:Thread.currentT...

  • iOS 获取工程内控制器名称

    OC 获取Xcode内所有的控制器类名方法 附:class与string之间的互相转换 通过类名获取类 通过类获取...

  • Appium常用api

    (1)获取当前页面的类名->可拿来做断言 1.通过adb 命令获取类名 2.通过api获取类名 (2)获取当前页面...

  • Runtime全面剖析之常用tips

    一: 什么是Runtime 类在runtime中的表示 1.1: 获取列表 有时候会有这样的需求,我们需要知道当前...

  • iOS中Runtime常用示例

    Runtime的内容大概有:动态获取类名、动态获取类的成员变量、动态获取类的属性列表、动态获取类的方法列表、动态获...

  • iOS-Runtime

    Runtime的内容大概有:动态获取类名、动态获取类的成员变量、动态获取类的属性列表、动态获取类的方法列表、动态获...

  • runtime基础

    目前我所了解的Runtime内容大约有:动态获取类名、动态获取类的成员变量、动态获取类的属性列表、动态获取类的方法...

  • iOS 获取当前控制器的正确方式

    iOS 获取当前控制器的正确方式 iOS 获取当前控制器的正确方式

网友评论

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

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