12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- //
- // UIViewController+ModalPresentationStyle.m
- // jiaPei
- //
- // Created by 张嵘 on 2020/3/14.
- // Copyright © 2020 JCZ. All rights reserved.
- //
- #import "UIViewController+ModalPresentationStyle.h"
- #import <objc/runtime.h>
- static const char *rq_automaticallySetModalPresentationStyleKey;
- @implementation UIViewController (ModalPresentationStyle)
- + (void)load {
- Method originAddObserverMethod = class_getInstanceMethod(self, @selector(presentViewController:animated:completion:));
- Method swizzledAddObserverMethod = class_getInstanceMethod(self, @selector(rq_presentViewController:animated:completion:));
- method_exchangeImplementations(originAddObserverMethod, swizzledAddObserverMethod);
- }
- - (void)setrq_automaticallySetModalPresentationStyle:(BOOL)rq_automaticallySetModalPresentationStyle {
- objc_setAssociatedObject(self, rq_automaticallySetModalPresentationStyleKey, @(rq_automaticallySetModalPresentationStyle), OBJC_ASSOCIATION_ASSIGN);
- }
- - (BOOL)rq_automaticallySetModalPresentationStyle {
- id obj = objc_getAssociatedObject(self, rq_automaticallySetModalPresentationStyleKey);
- if (obj) {
- return [obj boolValue];
- }
- return [self.class rq_automaticallySetModalPresentationStyle];
- }
- + (BOOL)rq_automaticallySetModalPresentationStyle {
- if ([self isKindOfClass:[UIImagePickerController class]] || [self isKindOfClass:[UIAlertController class]]) {
- return NO;
- }
- return YES;
- }
- - (void)rq_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
- if (@available(iOS 13.0, *)) {
- if (viewControllerToPresent.rq_automaticallySetModalPresentationStyle) {
- viewControllerToPresent.modalPresentationStyle = UIModalPresentationFullScreen;
- }
- [self rq_presentViewController:viewControllerToPresent animated:flag completion:completion];
- } else {
- // Fallback on earlier versions
- [self rq_presentViewController:viewControllerToPresent animated:flag completion:completion];
- }
- }
- - (UIModalPresentationStyle)modalPresentationStyle{
- return UIModalPresentationFullScreen;
- }
- @end
|