美文网首页常用方法
iOS横屏竖屏问题

iOS横屏竖屏问题

作者: 亦晴工作室 | 来源:发表于2016-11-14 15:41 被阅读220次

众所周知,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

相关文章

网友评论

    本文标题:iOS横屏竖屏问题

    本文链接:https://www.haomeiwen.com/subject/qmvhpttx.html