iOS强制转屏

作者: wnido | 来源:发表于2017-04-05 17:15 被阅读506次

引言

遇到需要转屏的功能,查资料刚好看到一篇iOS强制横屏或强制竖屏,不过发现里面的方法有点太不可取,做了下修改。


为什么push或者pop后的viewController 不响应所有的 旋转方法?

这里是因为,根视图(rootController)不是你的viewController,你去监听肯定是没有的,这里你就需要监听根视图,比如说下面的例子中的nav。


BaseNavCtl ---- 这代码里,是为了让MultiQualityViewController横屏,这里就举这一个栗子

#import <UIKit/UIKit.h>

@interface BaseNavCtl : UINavigationController

@end

BaseNavCtl .m

    //
    //  BaseNavCtl.m
    //  MGMediaDemo
    //
    //  Created by ciome on 2017/4/5.
    //  Copyright © 2017年 MiGu. All rights reserved.
    //
    
    #import "BaseNavCtl.h"
    #import "MultiQualityViewController.h"
    
    @interface BaseNavCtl ()
    
    
    @end
    
    @implementation BaseNavCtl
    
    - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    
    // 获取topCtl
    UIViewController  *ctl = self.topViewController;
    // 进行后续区分
    if([ctl isKindOfClass:[MultiQualityViewController class]]){
        
        return UIInterfaceOrientationMaskLandscape;
    }
    else {
        
        return UIInterfaceOrientationMaskAll;
    }
}

- (BOOL)shouldAutorotate
{
    return true;
}

 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // 获取topCtl
    UIViewController  *ctl = viewController;
    // 在进入之前强制旋转设备
    if([ctl isKindOfClass:[MultiQualityViewController class]]){
        
        [self interfaceOrientation:UIInterfaceOrientationLandscapeRight];
    }
    [super pushViewController:viewController animated:animated];
}



// 强制转屏 
- (void)interfaceOrientation:(UIInterfaceOrientation)orientation
{
    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 = orientation;
        // 从2开始是因为0 1 两个参数已经被selector和target占用
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

AppDelegate --- BaseNavCtl成为rootController

self.window= [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
BaseNavCtl *navigationController = [[BaseNavCtl alloc]initWithRootViewController:[[DemoMainViewController alloc]init]];
self.viewController= navigationController;
self.window.rootViewController=self.viewController;
[self.window makeKeyAndVisible];

为什么这样写

这样写的好处是,可以把整个APP内的视图方向管理,都集中在baseNavCtl这个类里面;当然,这样也必然增加了每次匹配的消耗,但由于每次nav只有一个topViewCtl ,所以可以做唯一判别减少匹配消耗,这里就不贴代码了。

相关文章

  • iOS强制转屏

    引言 遇到需要转屏的功能,查资料刚好看到一篇iOS强制横屏或强制竖屏,不过发现里面的方法有点太不可取,做了下修改。...

  • iOS 强制转屏

    由于最近在做一个实训项目,项目中有几个界面需要强制转屏于是乎就在网上找了一下关于iOS强制转屏的一些博客。 发现很...

  • iOS 强制转屏 强制横屏 强制竖屏

    今天项目中遇到正在看视频的时候账号被挤,如果当时是横屏的情况下,需要强制竖屏。真头疼,网上找了好多方法,终于解决啦...

  • iOS强制横屏

    iOS强制横屏

  • 横屏竖屏切换

    项目只有竖屏的时侯,在某个页面强制横屏模态的形式推入: 在此页面进行强制转屏 转屏页面 push:推入此类继承UI...

  • iOS 强制横屏(Push和模态)

    # iOS 强制横屏(Push和模态) iOS开发过程中,有时候需要页面强制横屏。 下面这种方法是不管手机有没有开...

  • iOS9 屏幕旋转

    需求:2个视图控制器一个强制横屏一个强制竖屏 1.无需自动转屏 AppDelegate.h: AppDelega...

  • iOS9之后强制横屏

    1、IOS8之后有的方法写到类里强制横屏之后已经没有用了 2、IOS8之后该怎么实现强制横屏 首先在代理类实现该方...

  • 编程技巧汇总

    iOS - 强制某个页面横屏,返回竖屏:https://www.jianshu.com/p/45ca13046ee...

  • 2022-03-05

    iOS16 强制横屏(竖屏)demo CSDN(0资源分)https://download.csdn.net/do...

网友评论

  • 89848af90932:这样管理是要好点,按你这个方法那pop的时候,是否也需要调用interfaceOrientation去强制回竖屏咯?(需求是回去保持竖屏)
    wnido:这只提供一种思路,具体使用要看你场景。 你强转横屏了,后回去,想转竖屏,肯定也要调interfaceOrientation 实现的

本文标题:iOS强制转屏

本文链接:https://www.haomeiwen.com/subject/gzlxattx.html