//
|
// UIViewController+ZFPlayerRotation.m
|
//
|
// Copyright (c) 2016年 任子丰 ( http://github.com/renzifeng )
|
//
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
// of this software and associated documentation files (the "Software"), to deal
|
// in the Software without restriction, including without limitation the rights
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
// copies of the Software, and to permit persons to whom the Software is
|
// furnished to do so, subject to the following conditions:
|
//
|
// The above copyright notice and this permission notice shall be included in
|
// all copies or substantial portions of the Software.
|
//
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
// THE SOFTWARE.
|
|
#import "UIViewController+ZFPlayerRotation.h"
|
#import <objc/runtime.h>
|
|
@implementation UITabBarController (ZFPlayerRotation)
|
|
+ (void)load {
|
static dispatch_once_t onceToken;
|
dispatch_once(&onceToken, ^{
|
SEL selectors[] = {
|
@selector(selectedIndex)
|
};
|
|
for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
|
SEL originalSelector = selectors[index];
|
SEL swizzledSelector = NSSelectorFromString([@"zf_" stringByAppendingString:NSStringFromSelector(originalSelector)]);
|
Method originalMethod = class_getInstanceMethod(self, originalSelector);
|
Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector);
|
if (class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))) {
|
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
|
} else {
|
method_exchangeImplementations(originalMethod, swizzledMethod);
|
}
|
}
|
});
|
}
|
|
- (NSInteger)zf_selectedIndex {
|
NSInteger index = [self zf_selectedIndex];
|
if (index > self.viewControllers.count) { return 0; }
|
return index;
|
}
|
|
/**
|
* If the root view of the window is a UINavigationController, you call this Category first, and then UIViewController called.
|
* All you need to do is revisit the following three methods on a page that supports directions other than portrait.
|
*/
|
|
// Whether automatic screen rotation is supported.
|
- (BOOL)shouldAutorotate {
|
UIViewController *vc = self.viewControllers[self.selectedIndex];
|
if ([vc isKindOfClass:[UINavigationController class]]) {
|
UINavigationController *nav = (UINavigationController *)vc;
|
return [nav.topViewController shouldAutorotate];
|
} else {
|
return [vc shouldAutorotate];
|
}
|
}
|
|
// Which screen directions are supported.
|
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
|
UIViewController *vc = self.viewControllers[self.selectedIndex];
|
if ([vc isKindOfClass:[UINavigationController class]]) {
|
UINavigationController *nav = (UINavigationController *)vc;
|
return [nav.topViewController supportedInterfaceOrientations];
|
} else {
|
return [vc supportedInterfaceOrientations];
|
}
|
}
|
|
// 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).
|
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
|
UIViewController *vc = self.viewControllers[self.selectedIndex];
|
if ([vc isKindOfClass:[UINavigationController class]]) {
|
UINavigationController *nav = (UINavigationController *)vc;
|
return [nav.topViewController preferredInterfaceOrientationForPresentation];
|
} else {
|
return [vc preferredInterfaceOrientationForPresentation];
|
}
|
}
|
|
@end
|
|
@implementation UINavigationController (ZFPlayerRotation)
|
|
/**
|
* If the root view of the window is a UINavigationController, you call this Category first, and then UIViewController called.
|
* All you need to do is revisit the following three methods on a page that supports directions other than portrait.
|
*/
|
|
// Whether automatic screen rotation is supported
|
- (BOOL)shouldAutorotate {
|
return [self.topViewController shouldAutorotate];
|
}
|
|
// Which screen directions are supported
|
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
|
return [self.topViewController supportedInterfaceOrientations];
|
}
|
|
// 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).
|
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
|
return [self.topViewController preferredInterfaceOrientationForPresentation];
|
}
|
|
- (UIViewController *)childViewControllerForStatusBarStyle {
|
return self.topViewController;
|
}
|
|
- (UIViewController *)childViewControllerForStatusBarHidden {
|
return self.topViewController;
|
}
|
|
@end
|