[iOS]代码控制APP中视图横屏/竖屏切换

很多时候,项目中都有这样的需求:APP中以竖屏为主,个别界面会要求横屏显示,或者要根据用户的手机朝向自动切换横竖屏;下面就来详细讲解,在项目中怎么使用代码来控制APP的界面转换.
首先,要想APP支持多个方向,需要在工程进行设置支持的朝向:
General-->Deployment Info-->Device Orientation中进行设置

设置APP支持的手机方向

这样,就可以在项目中使用代码来控制页面的朝向了,在这之前,需要知道控制视图是否能够自动旋转,以及支持的朝向,是通过哪些方法来控制的?
其实,主要使用的是下面三个方法:

// New Autorotation support.  
//是否自动旋转,返回YES可以自动旋转  
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;  
//返回支持的方向  
- (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;  
// Returns interface orientation masks.  
//这个是返回优先方向  
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;  

一般情况下,实现前两个方法即可!这些都是UIViewController的实例方法,直接在需要设置的控制器内重写上面的方法即可;
有时候,你会发现,只有在APP启动时出现的第一个界面内重写上面的两个方法,会有效果;而在其他的界面重写,并没有效果.这是因为,程序启动后的第一个界面是程序的跟视图控制器,而这些设置,必须是影响了跟视图中的相应设置,才会有效果;
假如,你在跟视图中进行了如下设置:

- (BOOL)shouldAutorotate{  
    return NO;  
}  

那么,在整个项目中,无论你如何设置,都不会有自动旋转屏幕的效果.
综上可知,如果要改变某个视图屏幕方向,需要告诉跟视图,在跟视图中修改为相应的设置即可;
所以,问题来了,怎么告诉跟视图呢?这要视情况而定:

1.当前viewController就是跟视图控制器

这种情况最简单,直接在控制器中重写上面的方法,设置是否支持自动旋转,以及支持的方向即可:

- (BOOL)shouldAutorotate{
    return YES;
}

#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0
- (NSUInteger)supportedInterfaceOrientations
#else
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
#endif
{
    return UIInterfaceOrientationMaskAll;
}

像上面我设置为支持自动旋转,支持所有的朝向;
这种方式, 切换视图的方式主要是模态(present), 只需要在相应的UIViewController里重写上面的方法即可.
但是这种情况很少见...
对应的demo为:LZInterfaceRotation中的Rotation-UIViewController

2.项目的跟视图控制器是导航(UINavigationController)

这种情况比较常见,也很简单;一般使用的导航的时候,都不会去直接使用系统的UINavigationController,而会定义一个他的子类,做一些公共的设置:

#import <UIKit/UIKit.h>

@interface BaseNaviViewController : UINavigationController

@end

然后在导航的viewController里面重写上面的三个方法,这里也有两种方式来控制:

2.1 设置最后一个push的viewController

这种方式,主要是用在,当push到某个视图控制器时,要求他能够支持自动旋转,或者强制转为横屏;这里主要用到了导航的viewControllers属性,这是一个数组,数组的最后一个元素,就是最后push进去的viewController,所以,实现代码如下:

-(BOOL)shouldAutorotate  
{  
    return [[self.viewControllers lastObject] shouldAutorotate];  
      
}  
  
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0  
- (NSUInteger)supportedInterfaceOrientations  
#else  
- (UIInterfaceOrientationMask)supportedInterfaceOrientations  
#endif  
{  
     return [[self.viewControllers lastObject] supportedInterfaceOrientations];  
}  
  
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {  
      
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];  
}  

到这里还没有结束,这里只是获取到了最后一个push的viewController,然后用它去调用了相应的方法,具体的方法实现,还是需要在哪个viewController里进行设置的,也就是在需要特殊设置的viewController里重写上面的方法:

-(BOOL)shouldAutorotate
{
    return YES;
}

#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0
- (NSUInteger)supportedInterfaceOrientations
#else
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
#endif
{
    return UIInterfaceOrientationMaskPortraitUpsideDown;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    
    return UIInterfaceOrientationPortraitUpsideDown;
}

其实这样也就是告诉跟视图控制器,这个界面我要特殊设置一下;
对应的demo为: LZInterfaceRotation中的Rotation-UINavigationController

2.2设置指定的控制器

第一种方法是,只要push进去一个视图控制器,都可以进行相应的设置,而这种方法是只针对特定的控制器进行设置,例如这里,我需要把SecondViewController单独设置为支持横屏,只需要将第一种方法中的导航里的相应代码做如下修改:

-(BOOL)shouldAutorotate
{
    if ([[self.viewControllers lastObject]isKindOfClass:[SecondViewController class]]) {
        return YES;
    }
    
    return NO;
}

#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0
- (NSUInteger)supportedInterfaceOrientations
#else
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
#endif
{
    if ([[self.viewControllers lastObject]isKindOfClass:[SecondViewController class]]) {
                return UIInterfaceOrientationMaskLandscapeLeft;
            }

    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    
    if ([[self.viewControllers lastObject]isKindOfClass:[SecondViewController class]]) {
        return UIInterfaceOrientationLandscapeLeft;
    }

    return UIInterfaceOrientationPortrait;
}

这样的好处是,可以控制指定的视图控制器,也不用在指定的视图控制器内再重写上面的方法;
坏处就是,如果需要控制的视图控制器比较多的话,加的判断会多很多;没有方式一灵活;
对应的demo为: LZInterfaceRotation中的Rotation-UINavigationController副本

3.跟视图控制器是UITabbarController

这种情况比较常见,项目的跟视图控制器是一个UITabBarController,这种情况的实现相对来说要复杂一些,但是,明白了原理,总是有方法来实现的.归根结底,也就是告诉跟视图,我这个界面需要支持旋转了;

第一步: 自定义TabbarController中重写对应方法

- (BOOL)shouldAutorotate {
    
    UIViewController *selectedVC = [self selectedViewController];
    return selectedVC.shouldAutorotate;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    
    UIViewController *selectedVC = [self selectedViewController];
    return selectedVC.supportedInterfaceOrientations;
}

PS: 如果你只想处理某些类型的控制器,可在这里加上类型判断,比如:我的tabbarController中使用的是自定义的导航控制器:BaseNavigationController

- (BOOL)shouldAutorotate {
    
    UIViewController *selectedVC = [self selectedViewController];
    
    if ([selectedVC isKindOfClass:[BaseNavigationController class]]) {
        BaseNavigationController *navi = (BaseNavigationController *)selectedVC;
        return navi.shouldAutorotate;
    }
    
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    
    UIViewController *selectedVC = [self selectedViewController];

    if ([selectedVC isKindOfClass:[BaseNavigationController class]]) {
        BaseNavigationController *navi = (BaseNavigationController *)selectedVC;
        return navi.supportedInterfaceOrientations;
    }
    
    return UIInterfaceOrientationMaskPortrait;
//    return UIInterfaceOrientationMaskAllButUpsideDown;
}

第二步:自定义导航类中重写方法
PS:如果没有自定义导航控制器基类,可跳过该步骤;

- (BOOL)shouldAutorotate {
    
    UIViewController *selectedVC = [self visibleViewController];
    return selectedVC.shouldAutorotate;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    
    UIViewController *selectedVC = [self visibleViewController];
    return selectedVC.supportedInterfaceOrientations;
}

PS: 如果你只想处理某些类型的控制器,可在这里加上类型判断,比如:我的导航控制器中使用的是自定义的控制器:BaseViewController

- (BOOL)shouldAutorotate {
    
    UIViewController *selectedVC = [self visibleViewController];
    
    if ([selectedVC isKindOfClass:[BaseViewController class]]) {
        BaseViewController *vc = (BaseViewController *)selectedVC;
        return vc.shouldAutorotate;
    }
    
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    
    UIViewController *selectedVC = [self visibleViewController];

    if ([selectedVC isKindOfClass:[BaseViewController class]]) {
        BaseViewController * vc = (BaseViewController *)selectedVC;
        return vc.supportedInterfaceOrientations;
    }
    
    return UIInterfaceOrientationMaskPortrait;
//    return UIInterfaceOrientationMaskAllButUpsideDown;
}

第三步:在需要旋转的控制中重写

- (BOOL)shouldAutorotate {
    
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

大功告成!!!

附加

最后介绍一个强制旋转屏幕的方法,这个方法可以在进入某个视图时,强制转成你需要的屏幕方向,用的比较多的是在一个竖屏的应用中强制转换某一个界面为横屏(例如播放视频):

PS: 这个方法的使用有个前提, 一定是在跟视图的-(BOOL)shouldAutorotate返回值为YES的时候才有效.

//强制旋转屏幕  
- (void)orientationToPortrait:(UIInterfaceOrientation)orientation {  
    SEL selector = NSSelectorFromString(@"setOrientation:");  
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];  
    [invocation setSelector:selector];  
    [invocation setTarget:[UIDevice currentDevice]];  
    int val = orientation;  
    [invocation setArgument:&val atIndex:2];//前两个参数已被target和selector占用  
    [invocation invoke];  
      
}  

使用的时候,只需要把你需要旋转的方向传过去即可!
有一点需要注意:从A进入B的时候, 把B强制转换成横屏,返回的时候,需要在A出现的时候再转换为原来的方向,不然会有问题;个人建议可以在BviewWillAppear调用这个方法,转换屏幕(例如转换为横屏),然后在AviewWillAppear中转换回来; 如果使用通知来告诉跟视图是否可以自动旋转, 这个方法要在通知发送之后调用.

具体效果及设置, 可参考demo: LZInterfaceRotation

(完)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容