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

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

作者: 不差钱儿的农民工 | 来源:发表于2018-08-06 09:57 被阅读0次

    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;

    }

    相关文章

      网友评论

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

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