UIViewController+ModalPresentationStyle.m 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // UIViewController+ModalPresentationStyle.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2020/3/14.
  6. // Copyright © 2020 JCZ. All rights reserved.
  7. //
  8. #import "UIViewController+ModalPresentationStyle.h"
  9. #import <objc/runtime.h>
  10. static const char *rq_automaticallySetModalPresentationStyleKey;
  11. @implementation UIViewController (ModalPresentationStyle)
  12. + (void)load {
  13. Method originAddObserverMethod = class_getInstanceMethod(self, @selector(presentViewController:animated:completion:));
  14. Method swizzledAddObserverMethod = class_getInstanceMethod(self, @selector(rq_presentViewController:animated:completion:));
  15. method_exchangeImplementations(originAddObserverMethod, swizzledAddObserverMethod);
  16. }
  17. - (void)setrq_automaticallySetModalPresentationStyle:(BOOL)rq_automaticallySetModalPresentationStyle {
  18. objc_setAssociatedObject(self, rq_automaticallySetModalPresentationStyleKey, @(rq_automaticallySetModalPresentationStyle), OBJC_ASSOCIATION_ASSIGN);
  19. }
  20. - (BOOL)rq_automaticallySetModalPresentationStyle {
  21. id obj = objc_getAssociatedObject(self, rq_automaticallySetModalPresentationStyleKey);
  22. if (obj) {
  23. return [obj boolValue];
  24. }
  25. return [self.class rq_automaticallySetModalPresentationStyle];
  26. }
  27. + (BOOL)rq_automaticallySetModalPresentationStyle {
  28. if ([self isKindOfClass:[UIImagePickerController class]] || [self isKindOfClass:[UIAlertController class]]) {
  29. return NO;
  30. }
  31. return YES;
  32. }
  33. - (void)rq_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
  34. if (@available(iOS 13.0, *)) {
  35. if (viewControllerToPresent.rq_automaticallySetModalPresentationStyle) {
  36. viewControllerToPresent.modalPresentationStyle = UIModalPresentationFullScreen;
  37. }
  38. [self rq_presentViewController:viewControllerToPresent animated:flag completion:completion];
  39. } else {
  40. // Fallback on earlier versions
  41. [self rq_presentViewController:viewControllerToPresent animated:flag completion:completion];
  42. }
  43. }
  44. - (UIModalPresentationStyle)modalPresentationStyle{
  45. return UIModalPresentationFullScreen;
  46. }
  47. @end