设置某个页面为横屏模式及横屏模式下弹框闪退问题

1.某个页面横屏显示

在appdelegate的代理方法supportedInterfaceOrientationsForWindow中添加相关代码:

//AppDelegate.h中

@interfaceAppDelegate :UIResponder

@property (nonatomic,assign)NSInteger allowRotation;//是否允许横屏

@end



//AppDelegate.m中

@implementation AppDelegate

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

{

    if (_allowRotation == 1)  {

        return UIInterfaceOrientationMaskAll;

    }

    else{

        return (UIInterfaceOrientationMaskPortrait);

    }

}

@end



需要旋转的controller中添加如下:

@implementation  HorizontalViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    [selfcreadUI];

    AppDelegate* appDelegate = (AppDelegate*)[UIApplicationsharedApplication].delegate;

    appDelegate.allowRotation=1;

}

//点击返回按钮退出页面时恢复竖屏方式

- (void)back:(UIButton*)sender

{

    AppDelegate* appDelegate = (AppDelegate*)[UIApplicationsharedApplication].delegate;

    appDelegate.allowRotation=0;

    [self dismissViewControllerAnimated:YES completion:nil];

}

@end


点击按钮,以模态视图的方式弹出横屏页面:

- (IBAction)signatureImageDidSelect:(id)sender {

    WriteViewController *writeVc =[[WriteViewController alloc] init];

    [self presentViewController:writeVc animated:YES completion:nil];

}


2.横屏下弹框闪退问题处理

Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and [UIAlertController shouldAutorotate] is returning YES'

防止横屏情况下发生的闪退情况,在需要横屏的controller中添加如下代码:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight );

}

-(NSUInteger)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskLandscape;

}

- (BOOL)shouldAutorotate

{

    return NO;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 你若盛开,清风不再。 ——题记 时光如白煦过隙,而记忆从未搁浅。...
    江城小生阅读 2,990评论 2 0
  • 【开通QQ空间;关闭QQ空间;QQ群和好友恢复;QQ空间解除禁言;QQ好友在线克隆;解封保护模式;QQ冻结申诉;手...
    鸽鸽i阅读 1,702评论 0 0
  • 2017年12月8日我终于看了期待已久的《致爱梵高》这部电影,整部电影是围绕梵高死因之谜来展开。你可能会认为梵高是...
    九玉子阅读 7,253评论 0 0
  • 华蓥双枪名远扬, 沫若题词情深长。 情山作伴归故乡, 一捧相思寄情郎。
    向火火阅读 2,701评论 0 1

友情链接更多精彩内容