iOS 指定页面支持横竖屏切换

项目要在配置里只支持竖屏

1.首先在appdelgate里添加一个属性

@property(nonatomic, assign) BOOL allowRotation; 

2.在.m文件中添加一下两个方法

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (self.allowRotation == 1) {
        return UIInterfaceOrientationMaskAll;
    }else{
        return (UIInterfaceOrientationMaskPortrait);
    }
}

// 支持设备自动旋转
- (BOOL)shouldAutorotate
{
    if (self.allowRotation == 1) {
        return YES;
    }
    return NO;
}

3.在想要支持横屏的控制器中添加一下代码

    AppDelegate  *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.allowRotation = 1;

这样就可以实现在指定界面进行横竖屏切换了,退出当前界面的时候 肯定要回到竖屏 , 此时在控制器将要消失的时候添加代码

    AppDelegate  *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.allowRotation = 0;

if ([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]) {
        [self orientationToPortrait:UIInterfaceOrientationPortrait];
    }

还要自己写一个方法在当前的控制器页面的.m文件里

// 防止更改设备的横竖屏不起作用
-(void)orientationToPortrait:(UIInterfaceOrientation)orientation{
    
    SEL seletor = NSSelectorFromString(@"setOrientation:");
    
    NSInvocation *invocatino = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:seletor]];
    [invocatino setSelector:seletor];
    [invocatino setTarget:[UIDevice currentDevice]];
    int val = orientation;
    [invocatino setArgument:&val atIndex:2];
    [invocatino invoke];
    
}

希望对你有帮助 ,若有疑问可联系675399100

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AGI阅读 16,012评论 3 119
  • 今天,是我来简书的第45天,也是我简书粉丝突破100的纪念日。尽管这是个丢人的数据,但我依然要拎出来说一下,不为炫...
    亚姐在东京阅读 576评论 17 15
  • SSL是网景公司发布的协议,后来被互联网标准化组织接手,升级并重命名为TLS https中的s就是sslSSL为了...
    DoraLvor阅读 538评论 0 0
  • 爷爷奶奶年纪大,外公外婆身体不好,育儿路上双方父母都没有办法给予帮助。自我休完四个月产假到孩子上幼儿园,先后请了两...
    邵小絮_5737阅读 296评论 2 0
  • 最近疲于奔波,心理上脑力上,越来越没有时间去娱乐休闲。可能算是一种成长吧。 从昨天手机开始出现问题,就陷入了慌乱,...
    小花Ivan阅读 229评论 0 0