UIViewController+ZFPlayerRotation.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // UIViewController+ZFPlayerRotation.m
  3. //
  4. // Copyright (c) 2016年 任子丰 ( http://github.com/renzifeng )
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. #import <UIKit/UIKit.h>
  24. #import <objc/runtime.h>
  25. @implementation UITabBarController (ZFPlayerRotation)
  26. + (void)load {
  27. static dispatch_once_t onceToken;
  28. dispatch_once(&onceToken, ^{
  29. SEL selectors[] = {
  30. @selector(selectedIndex)
  31. };
  32. for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
  33. SEL originalSelector = selectors[index];
  34. SEL swizzledSelector = NSSelectorFromString([@"zf_" stringByAppendingString:NSStringFromSelector(originalSelector)]);
  35. Method originalMethod = class_getInstanceMethod(self, originalSelector);
  36. Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector);
  37. if (class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))) {
  38. class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
  39. } else {
  40. method_exchangeImplementations(originalMethod, swizzledMethod);
  41. }
  42. }
  43. });
  44. }
  45. - (NSInteger)zf_selectedIndex {
  46. NSInteger index = [self zf_selectedIndex];
  47. if (index > self.viewControllers.count) return 0;
  48. return index;
  49. }
  50. /**
  51. * If the root view of the window is a UINavigationController, you call this Category first, and then UIViewController called.
  52. * All you need to do is revisit the following three methods on a page that supports directions other than portrait.
  53. */
  54. // Whether automatic screen rotation is supported.
  55. - (BOOL)shouldAutorotate {
  56. UIViewController *vc = self.viewControllers[self.selectedIndex];
  57. if ([vc isKindOfClass:[UINavigationController class]]) {
  58. UINavigationController *nav = (UINavigationController *)vc;
  59. return [nav.topViewController shouldAutorotate];
  60. } else {
  61. return [vc shouldAutorotate];
  62. }
  63. }
  64. // Which screen directions are supported.
  65. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  66. UIViewController *vc = self.viewControllers[self.selectedIndex];
  67. if ([vc isKindOfClass:[UINavigationController class]]) {
  68. UINavigationController *nav = (UINavigationController *)vc;
  69. return [nav.topViewController supportedInterfaceOrientations];
  70. } else {
  71. return [vc supportedInterfaceOrientations];
  72. }
  73. }
  74. // The default screen direction (the current ViewController must be represented by a modal UIViewController (which is not valid with modal navigation) to call this method).
  75. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  76. UIViewController *vc = self.viewControllers[self.selectedIndex];
  77. if ([vc isKindOfClass:[UINavigationController class]]) {
  78. UINavigationController *nav = (UINavigationController *)vc;
  79. return [nav.topViewController preferredInterfaceOrientationForPresentation];
  80. } else {
  81. return [vc preferredInterfaceOrientationForPresentation];
  82. }
  83. }
  84. @end
  85. @implementation UINavigationController (ZFPlayerRotation)
  86. /**
  87. * If the root view of the window is a UINavigationController, you call this Category first, and then UIViewController called.
  88. * All you need to do is revisit the following three methods on a page that supports directions other than portrait.
  89. */
  90. // Whether automatic screen rotation is supported
  91. - (BOOL)shouldAutorotate {
  92. return [self.topViewController shouldAutorotate];
  93. }
  94. // Which screen directions are supported
  95. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  96. return [self.topViewController supportedInterfaceOrientations];
  97. }
  98. // The default screen direction (the current ViewController must be represented by a modal UIViewController (which is not valid with modal navigation) to call this method).
  99. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  100. return [self.topViewController preferredInterfaceOrientationForPresentation];
  101. }
  102. - (UIViewController *)childViewControllerForStatusBarStyle {
  103. return self.topViewController;
  104. }
  105. - (UIViewController *)childViewControllerForStatusBarHidden {
  106. return self.topViewController;
  107. }
  108. @end