ios整个app竖屏,个别界面横屏支持设置demo

先看一下效果图:

ios整个app竖屏,个别界面横屏支持设置demo_第1张图片

首先创建项目的时候要做支持横屏的设置:General->Deployment Info

ios整个app竖屏,个别界面横屏支持设置demo_第2张图片

1、通过delegate控制设置

在delegate里面定义一个全局变量 supportRotate ,然后在delegate中重写代码如下:

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (self.supportRotate) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }else{
        return UIInterfaceOrientationMaskPortrait;
    }
}

在需要横屏的界面重写代码如下:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.supportRotate = YES; //通过delegate来设置的方法
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.supportRotate = NO;
}

这个主要是通过delegate的回调来控制app横竖屏,每进入一个页面都会回调 application: supportedInterfaceOrientationsForWindow:这个方法,检查该界面支不支持横屏,这个方法比较简单,适用于不需要监听界面横竖屏切换状态的情景(比如优酷sdk集成的时候就需要在是否支持横屏的方法里面去重写他的一些方法这个就不适用了)


2、通过父视图的控制来设置

因为新版的横屏是一层层传递的,举个例子,你的根式图是 UITabBarController ,假如tabbar界面不支持横屏,点进去的每一个item控制器也必然不支持横屏。相当于上层直接禁用了这个功能,根本不会回调子界面的 shouldAutorotate方法,自然不可能支持横屏。

假如delegate中设置:
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;
    myTabBarController *tab = [[myTabBarController alloc]init];
    self.window.rootViewController = tab;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

tabbar中:
.h中定义一个 supportRotate,.m代码设置:
- (BOOL)shouldAutorotate {
    if (self.supportRotate) {
        return [self.selectedViewController shouldAutorotate];
    }else{
        return NO;
    }
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if (self.supportRotate) {
        return [self.selectedViewController supportedInterfaceOrientations];
    }else{
        return UIInterfaceOrientationMaskPortrait;
    }
}

这边tabbar的selectViewController是 UINavigationController ,调用 shouldAutorotate 方法时就会调用导航控制器的 shouldAutorotate方法, 所以我们要在导航控制器中作如下设置:
#pragma mark - 横屏相关
// New Autorotation support.
- (BOOL)shouldAutorotate {
    if (self.supportRotate) {
        return [self.topViewController shouldAutorotate];
    }else{
        return NO;
    }
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if (self.supportRotate) {
        return [self.topViewController supportedInterfaceOrientations];
    }else{
        return UIInterfaceOrientationMaskPortrait;
    }
}

假如父类调用[ self . selectedViewController   shouldAutorotate]这个方法,子类没有重新这个方法就会崩溃
在我们需要横屏的页面先设置tabbar supportRotate = YES ,再导航控制器的 supportRotate  =  YES,否则子类的横屏相关方法不会调用
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    myTabBarController *rootVC = (myTabBarController *)[[[UIApplication sharedApplication] keyWindow] rootViewController];
    rootVC.supportRotate = YES;
    
    MyNavViewController *nav = (MyNavViewController *)self.navigationController;
    nav.supportRotate = YES;
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    myTabBarController *rootVC = (myTabBarController *)[[[UIApplication sharedApplication] keyWindow] rootViewController];
    rootVC.supportRotate = NO;
    
    MyNavViewController *nav = (MyNavViewController *)self.navigationController;
    nav.supportRotate = NO;
}

附 下载Demo


你可能感兴趣的:(【开发技术】IOS)