UIViewController+ZFPlayerFixSafeArea.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //
  2. // UIViewController+ZFPlayerFixSafeArea.m
  3. // JSJP
  4. //
  5. // Created by 张嵘 on 2021/12/6.
  6. //
  7. #import "UIViewController+ZFPlayerFixSafeArea.h"
  8. #import <objc/message.h>
  9. #import <ZFPlayer/ZFPlayerController.h>
  10. BOOL zf_isFullscreenOfFixSafeArea = NO;
  11. API_AVAILABLE(ios(13.0)) @protocol _UIViewControllerPrivateMethodsProtocol <NSObject>
  12. - (void)_setContentOverlayInsets:(UIEdgeInsets)insets andLeftMargin:(CGFloat)leftMargin rightMargin:(CGFloat)rightMargin;
  13. @end
  14. @implementation UIViewController (ZFPlayerFixSafeArea)
  15. - (void)zf_setContentOverlayInsets:(UIEdgeInsets)insets andLeftMargin:(CGFloat)leftMargin rightMargin:(CGFloat)rightMargin {
  16. if (zf_isFullscreenOfFixSafeArea == NO) {
  17. [self zf_setContentOverlayInsets:insets andLeftMargin:leftMargin rightMargin:rightMargin];
  18. }
  19. }
  20. @end
  21. API_AVAILABLE(ios(13.0)) @implementation ZFOrientationObserver (ZFPlayerFixSafeArea)
  22. + (void)initialize {
  23. if ( @available(iOS 13.0, *) ) {
  24. static dispatch_once_t onceToken;
  25. dispatch_once(&onceToken, ^{
  26. Class cls = UIViewController.class;
  27. SEL originalSelector = @selector(_setContentOverlayInsets:andLeftMargin:rightMargin:);
  28. SEL swizzledSelector = @selector(zf_setContentOverlayInsets:andLeftMargin:rightMargin:);
  29. Method originalMethod = class_getInstanceMethod(cls, originalSelector);
  30. Method swizzledMethod = class_getInstanceMethod(cls, swizzledSelector);
  31. method_exchangeImplementations(originalMethod, swizzledMethod);
  32. Class pc_class = ZFPlayerController.class;
  33. SEL pc_originalSelector = @selector(enterFullScreen:animated:completion:);
  34. #pragma clang diagnostic push
  35. #pragma clang diagnostic ignored "-Wundeclared-selector"
  36. SEL pc_swizzledSelector = @selector(zf_enterFullScreen:animated:completion:);
  37. #pragma clang diagnostic pop
  38. Method pc_originalMethod = class_getInstanceMethod(pc_class, pc_originalSelector);
  39. Method pc_swizzledMethod = class_getInstanceMethod(pc_class, pc_swizzledSelector);
  40. method_exchangeImplementations(pc_originalMethod, pc_swizzledMethod);
  41. });
  42. }
  43. }
  44. @end
  45. API_AVAILABLE(ios(13.0)) @implementation ZFPlayerController (ZFPlayerFixSafeArea)
  46. - (void)zf_enterFullScreen:(BOOL)fullScreen animated:(BOOL)animated completion:(void (^ _Nullable)(void))completion {
  47. zf_isFullscreenOfFixSafeArea = fullScreen;
  48. [self zf_enterFullScreen:fullScreen animated:animated completion:completion];
  49. }
  50. @end