iOS禁止自动横竖屏切换!

在AppDelegate.h文件中添加

/***  是否允许横屏的标记 */
@property (nonatomic,assign)BOOL allowRotation;

在AppDelegate.m中添加

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (self.allowRotation) {//如果设置了allowRotation属性,支持全屏
       return UIInterfaceOrientationMaskAll;
}
    return UIInterfaceOrientationMaskPortrait;//默认全局不支持横屏
}

接着在要横屏的地方设置

//进入全屏
-(void)begainFullScreen{
  AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  appDelegate.allowRotation = YES;
}
// 退出全屏
-(void)endFullScreen{
  AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  appDelegate.allowRotation = NO;
  //强制归正:
  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 =UIInterfaceOrientationPortrait;
    [invocation setArgument:&val atIndex:2];
    [invocation invoke];
  }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.监听屏幕旋转方向 在处理iOS横竖屏时,经常会和UIDeviceOrientation、UIInterface...
    彬至睢阳阅读 7,276评论 1 6
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 14,188评论 2 33
  • 1,NSObject中description属性的意义,它可以重写吗?答案:每当 NSLog(@"")函数中出现 ...
    eightzg阅读 9,648评论 2 19
  • 端口转发的源主机ip:"source_ip"; 端口转发的目标主机ip:"destination_ip" 001 ...
    Col_阅读 9,334评论 0 1
  • 在Mac上使用Android Studio新建一个app项目时,发现按照引导步骤走完后,AS卡死在Building...
    GreyYang阅读 4,929评论 0 1