iOS13 横竖屏的切换

先吐槽下简书,本来昨天在简书中搜索到的文章https://www.jianshu.com/p/18115d353df9解决了问题。今天在来看文章没了, 作者也没了,,,,我勒个大擦。
此文章做个笔记,防止再找不到了。

项目配置中,我使用的是Portrait


截屏2020-07-16 上午10.04.04.png

appdelegate.m中

#pragma mark - 支持了竖屏和home在右侧的横屏
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{

    return UIInterfaceOrientationMaskPortrait |UIInterfaceOrientationMaskLandscapeRight;
}

再需要横竖屏切换的页面中

//#pragma mark - 以下3个方法横竖屏的切换,在BaseNavigationController中指定了此页面可以横屏
- (BOOL)shouldAutorotate
{
    return YES;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
   return UIInterfaceOrientationPortrait;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return  UIInterfaceOrientationMaskLandscapeRight |UIInterfaceOrientationMaskPortrait;
}

如果还不能实现横竖屏切换,是TabbarController和NavigationController也需要配置
注意我用的是
nav.m中

#pragma mark - 以下3个方法横竖屏的切换 是否自动旋转,返回YES可以自动旋转
- (BOOL)shouldAutorotate
{
    if ([self.topViewController respondsToSelector:@selector(shouldAutorotate)])
    {
        return [self.topViewController shouldAutorotate];
    }
    return NO;
}

//返回支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    if ([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientations)])
    {
        return [self.topViewController supportedInterfaceOrientations];
    }
    return UIInterfaceOrientationMaskPortrait;
}

//这个是返回优先方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    if ([self.topViewController respondsToSelector:@selector(preferredInterfaceOrientationForPresentation)])
    {
        return [self.topViewController preferredInterfaceOrientationForPresentation];
    }
    return UIInterfaceOrientationPortrait;
}

tabbar.m中

#pragma mark - 以下3个方法横竖屏的切换 是否自动旋转,返回YES可以自动旋转
- (BOOL)shouldAutorotate
{
    BaseNavigationController *nav = (BaseNavigationController *)self.selectedViewController;
    if ([nav isKindOfClass:[BaseNavigationController class]])
    {
        return [self.selectedViewController shouldAutorotate];
    }
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    BaseNavigationController *nav = (BaseNavigationController *)self.selectedViewController;
    if ([nav isKindOfClass:[BaseNavigationController class]])
    {
        return [self.selectedViewController supportedInterfaceOrientations];
    }
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    BaseNavigationController *nav = (BaseNavigationController *)self.selectedViewController;
    if ([nav isKindOfClass:[BaseNavigationController class]])
    {
        return [self.selectedViewController preferredInterfaceOrientationForPresentation];
    }
    return UIInterfaceOrientationPortrait;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。