iOS页面旋转详解

前言

在iOS开发中,如果APP需要支持横屏,就要控制页面旋转,但是让页面支持旋转的方式有很多,在此总结一下,说一下我对页面旋转的理解。

思路

控制页面旋转的方式可以总结为两种,第一种是通过全局设置来控制,第二种是页面自己单独控制。

1.修改全局设置来实现

第一种是通过勾选方向让页面支持旋转。

第二种是通过修改info.plist文件 Supported interface orientations设置选项,增加方向让页面支持旋转。

第三种是通过在 AppDelegate中实现 - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window方法让页面支持旋转。

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    //返回你需要的方向
    return UIInterfaceOrientationMaskPortrait;
}
复制代码

这三种方式种,前面两种是一样的,需要注意的是第三种方式,它的优先级是最高的,也就是你通过AppDelegate这种方式来控制全局旋转,同时勾选或者修改了plist选项,最终会以AppDelegate中支持的方向为准。

全局控制这种方式,通常用在所有页面都需要支持全屏的情况下,如果要让某个页面支持,大部分页面不支持,又该怎么处理呢?在这里利用runtime动态替换方法和分类的特性,来实现单独控制页面旋转,经过封装后,一句话就可以达到让页面支持或者不支持旋转。

代码

AppDelegate分类代码中实现- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window方法,利用分类的特性来完成AppDelegate需要实现的代码

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    //返回你需要的方向
    return UIInterfaceOrientationMaskPortrait;
}
复制代码

UIViewController分类中利用runtime动态替换方法实现控制页面旋转,这里使用week是因为页面销毁的时候需要将其他控制器的方向还原,不被当前页面修改方向后影响。

- (void)isNeedRotation:(BOOL)needRotation{
    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    __weak __typeof(self) weakSelf = self;
    IMP originalIMP = method_getImplementation(class_getInstanceMethod([appDelegate class], @selector(application:supportedInterfaceOrientationsForWindow:)));
    
    IMP newIMP = imp_implementationWithBlock(^(id obj, UIApplication *application, UIWindow *window){
        if (!weakSelf) {
            class_replaceMethod([appDelegate class], @selector(application:supportedInterfaceOrientationsForWindow:), originalIMP, method_getTypeEncoding(class_getInstanceMethod([appDelegate class], @selector(application:supportedInterfaceOrientationsForWindow:))));
        }
        return needRotation ? UIInterfaceOrientationMaskAll : UIInterfaceOrientationMaskPortrait;
    });
    
    class_replaceMethod([appDelegate class], @selector(application:supportedInterfaceOrientationsForWindow:), newIMP, method_getTypeEncoding(class_getInstanceMethod([appDelegate class], @selector(application:supportedInterfaceOrientationsForWindow:))));
}
复制代码
2.通过每个页面单独控制页面旋转

通过每个页面单独控制页面旋转,首先必须打开全局方向设置,设置需要旋转的方向,然后根据页面不同的创建方式(push或者present)和不同根控制器(UITabBarController或者UINavigationController),可以分出三种情况。

第一种情况,页面通过UINavigationController+push创建,在这种情况下,需要在UINavigationController实现以下方法就可以使页面支持旋转。

// 是否支持自动转屏
- (BOOL)shouldAutorotate {
    return [self.topViewController shouldAutorotate];
}
// 支持哪些屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [self.topViewController supportedInterfaceOrientations];
}
复制代码

第二种情况,页面通过UITabBarController+push创建,在这种情况下,需要在UITabBarControllerUINavigationController都实现以下方法才可以让页面支持旋转。其中需要注意的是UITabBarController中,需要利用runtime动态替换系统方法,防止没有初始值造成越界。

+ (void)load {
    SEL selectors[] = {
        @selector(selectedIndex)
    };
    for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
        SEL originalSelector = selectors[index];
        SEL swizzledSelector = NSSelectorFromString([@"cl_" 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)cl_selectedIndex {
    NSInteger index = [self cl_selectedIndex];
    if (index > self.viewControllers.count){
        return 0;
    }else{
        return index;
    }
}
复制代码
- (BOOL)shouldAutorotate {
 return [self.selectedViewController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations {
 return [self.selectedViewController supportedInterfaceOrientations];
}
复制代码

第三种情况,页面通过present创建,需要在present出来的页面实现以下方法才可以让页面支持旋转。

// 是否支持自动转屏
- (BOOL)shouldAutorotate {
    return NO;
}
// 支持哪些屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
// 默认的屏幕方向(当前ViewController必须是通过模态出来的UIViewController(模态带导航的无效)方式展现出来的,才会调用这个方法)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}
复制代码

页面自己单独控制旋转,需要每个页面都写重复的代码,很多时候都是利用基类来实现,但是需要子页面继承,对代码还是有一定的影响。既不想写基类,又不想每个页面单独写代码,又该怎么来实现呢?在这里还是利用分类的特性,分别创建UITabBarControllerUINavigationControllerUIViewController的分类来实现。

代码

UITabBarController分类中的代码。

// 是否支持自动转屏
- (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];
    }
}
// 支持哪些屏幕方向
- (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];
    }
}
// 默认的屏幕方向(当前ViewController必须是通过模态出来的UIViewController(模态带导航的无效)方式展现出来的,才会调用这个方法)
- (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];
    }
}
复制代码

UINavigationController分类中的代码。

// 是否支持自动转屏
- (BOOL)shouldAutorotate {
    return [self.topViewController shouldAutorotate];
}
// 支持哪些屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [self.topViewController supportedInterfaceOrientations];
}
// 默认的屏幕方向(当前ViewController必须是通过模态出来的UIViewController(模态带导航的无效)方式展现出来的,才会调用这个方法)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}
复制代码

UIViewController分类中的代码。

/**
 * 默认所有都不支持转屏,如需个别页面支持除竖屏外的其他方向,请在viewController重新下边这三个方法
 */
// 是否支持自动转屏
- (BOOL)shouldAutorotate {
    return NO;
}
// 支持哪些屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
// 默认的屏幕方向(当前ViewController必须是通过模态出来的UIViewController(模态带导航的无效)方式展现出来的,才会调用这个方法)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}
复制代码
总结

基于以上两种情况,我都分别写了Demo,具体大家自己去看Demo,如果喜欢,欢迎start。Demo地址 CLRotationTools

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值