众所周知,App 默认的方向都是支持左横屏、右横屏和竖屏的。但是在集成视频播放器的时候会用到横屏,那么问题来了。
当视频播放完成以后,那么其他界面可能也会横屏。为了解决这个问题,可以使用下面的解决办法。
RotationSupport.h
#import <UIKit/UIKit.h>
@interface UIViewController (Rotation)
-(BOOL)shouldAutorotate;
-(UIInterfaceOrientationMask)supportedInterfaceOrientations;
@end
@interface UITabBarController (Rotation)
-(BOOL)shouldAutorotate;
-(UIInterfaceOrientationMask)supportedInterfaceOrientations;
@end
@interface UINavigationController (Rotation)
-(BOOL)shouldAutorotate;
-(UIInterfaceOrientationMask)supportedInterfaceOrientations;
@end
RotationSupport.m
Tip:StudyPushViewController.h 和SubPushViewController.h是播放视频的界面
SecondNVController.h是跳转进入播放器的导航栏
#import "RotationSupport.h"
#import "StudyPushViewController.h"
#import "SubPushViewController.h"
#import "SecondNVController.h"
@implementation UIViewController (Rotation)
- (BOOL)shouldAutorotate
{
// ViewController 设置为支持自动转屏
if ([self isKindOfClass:[SubPushViewController class]]||[self isKindOfClass:[StudyPushViewController class]]) {
return YES;
}
return NO;
}
// 支持旋转屏幕的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
// ViewController设置支持旋转的方向
// ViewController 设置为支持自动转屏
if ([self isKindOfClass:[SubPushViewController class]]||[self isKindOfClass:[StudyPushViewController class]]) {
return UIInterfaceOrientationMaskAllButUpsideDown;
}else {
return UIInterfaceOrientationMaskPortrait;
}
return UIInterfaceOrientationMaskPortrait;
}
@end
@implementation UITabBarController (Rotation)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
@end
@implementation UINavigationController (Rotation)
- (BOOL)shouldAutorotate
{
// ViewController 设置为支持自动转屏
if ([self isKindOfClass:[SecondNVController class]]) {
NSInteger arrCount = self.viewControllers.count;
if ([self.viewControllers[arrCount-1] isKindOfClass:[SubPushViewController class]]||[self.viewControllers[arrCount-1] isKindOfClass:[StudyPushViewController class]]) {
return YES;
}
}
return NO;
}
// 支持旋转屏幕的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
// ViewController设置支持旋转的方向
// ViewController 设置为支持自动转屏
if ([self isKindOfClass:[SecondNVController class]]) {
NSInteger arrCount = self.viewControllers.count;
if ([self.viewControllers[arrCount-1] isKindOfClass:[SubPushViewController class]]||[self.viewControllers[arrCount-1] isKindOfClass:[StudyPushViewController class]]) {
return UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight|UIInterfaceOrientationMaskPortrait;
}else {
return UIInterfaceOrientationMaskPortrait;
}
}
return UIInterfaceOrientationMaskPortrait;
}
@end