ios13 横竖屏切换

这篇文章是关于在一个页面内的横竖屏切换(网上找了一堆都不太适用,问了朋友加上自己修改结合的方法):

首先我的设备方向是固定竖屏的:

ios13 横竖屏切换_第1张图片
image

1.在AppDelegate.m里添加你需要支持的方向:

我这里支持了竖屏和home在右侧的横屏


-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{

    return UIInterfaceOrientationMaskPortrait |UIInterfaceOrientationMaskLandscapeRight;
}

之所以不在General里直接勾选支持而在AppDelegate.m里添加,是可以避免掉launchscreen.storyboard横屏,如果你的storyboard支持横屏的话,可以不添加上面的代码,直接在General里添加支持就可以

2.在需要切换屏幕的页面添加支持:

- (BOOL)shouldAutorotate
{
    return YES;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
   return UIInterfaceOrientationPortrait;
}

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

3.调用横屏代码

if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
           SEL selector             = NSSelectorFromString(@"setOrientation:");
           NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
           [invocation setSelector:selector];
           [invocation setTarget:[UIDevice currentDevice]];
           int val                  = UIInterfaceOrientationLandscapeRight;
           [invocation setArgument:&val atIndex:2];
           [invocation invoke];
       }
       [UIViewController attemptRotationToDeviceOrientation];
}

后续发现在某些真机的时候未生效,分析应该是Nav和Tabbar的支持没有写入,放一下我自己的Nav和Tabbar代码

  1. MainNav.h
#import 

NS_ASSUME_NONNULL_BEGIN

@interface MainNav : UINavigationController

@end

NS_ASSUME_NONNULL_END

2.MainNav.m

#import "MainNav.h"

@interface MainNav ()

@end

@implementation MainNav

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

//是否自动旋转,返回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;
}

@end

3.mainTabBar.h

#import 

NS_ASSUME_NONNULL_BEGIN

@interface MainTabBar : UITabBarController

@end

NS_ASSUME_NONNULL_END

4.MainTabBar.m

#import "MainTabBar.h"
#import "ShuMuVC.h"
#import "ZhiBoVC.h"
#import "TwoMineVC.h"
#import "HomePageG/HomeVC.h"
#import "MainNav.h"

@interface MainTabBar ()

@end

@implementation MainTabBar

- (void)viewDidLoad {
    [super viewDidLoad];
    HomeVC *homevc = [[HomeVC alloc]init];
    MainNav *homeNav = [[MainNav alloc]initWithRootViewController:homevc];
    homeNav.tabBarItem.image = [[UIImage imageNamed:@"tabbar_home"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    homeNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_home_s"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    homeNav.title = @"首 页";
    
    
    
    ZhiBoVC *yunsjvc = [[ZhiBoVC alloc]init];
    MainNav *yunsjNav = [[MainNav alloc]initWithRootViewController:yunsjvc];
    yunsjNav.tabBarItem.image = [[UIImage imageNamed:@"tabbar_live"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    yunsjNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_live_s"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    yunsjNav.title = @"视 频";
    
    
    //CaiFVC *caifuvc = [[CaiFVC alloc]init];
    ShuMuVC *caifuvc = [[ShuMuVC alloc]init];
    MainNav *caifuNav = [[MainNav alloc]initWithRootViewController:caifuvc];
    caifuNav.tabBarItem.image = [[UIImage imageNamed:@"tabbar_book"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    caifuNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_book_s"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    caifuNav.title = @"书 目";
    
    TwoMineVC *minevc = [[TwoMineVC alloc]init];
    MainNav *mineNav = [[MainNav alloc]initWithRootViewController:minevc];
    mineNav.tabBarItem.image = [[UIImage imageNamed:@"tabbar_mine"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    mineNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_mine_s"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    mineNav.title = @"我 的";
    
    NSArray *navary = @[homeNav,yunsjNav,caifuNav,mineNav];
    self.viewControllers = navary;
}

- (BOOL)shouldAutorotate
{
    MainNav *nav = (MainNav *)self.selectedViewController;
    if ([nav isKindOfClass:[MainNav class]])
    {
        return [self.selectedViewController shouldAutorotate];
    }
    return NO;
}

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    MainNav *nav = (MainNav *)self.selectedViewController;
    if ([nav isKindOfClass:[MainNav class]])
    {
        return [self.selectedViewController preferredInterfaceOrientationForPresentation];
    }
    return UIInterfaceOrientationPortrait;
}

@end

TabBar里换成自己的页面就可以了.

你可能感兴趣的:(ios13 横竖屏切换)