【问题标题】:App is ignoring my orientation when it changed应用程序在更改时忽略了我的方向
【发布时间】:2016-04-24 15:30:37
【问题描述】:

我有问题。我在这里解决了这个问题(https://stackoverflow.com/questions/34870418/app-crashes-if-the-orientation-is-changing-in-uiwebview

但现在我的应用忽略了我的orientation。如果我输入了登录数据并加载了新网站但orientation 不一样,则应用程序将忽略新的orientation

dispatch_async(dispatch_get_main_queue(), ^{
    NSNumber *value = [NSNumber alloc];
    NSLog(@"Inside!");
    NSLog(@"Orientation: %@", [self orientNr]);
    if( [[self orientNr] isEqualToNumber:@1] ) value = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
    else if( [[self orientNr] isEqualToNumber:@2] ) value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
    else if( [[self orientNr] isEqualToNumber:@3] ) value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
});

该应用程序正在此块内,因为我在控制台上获得了两个日志。 orientation 是正确的,但它不会执行。

为什么应用忽略了我的orientation?如果我重新启动应用程序,它总是纵向的,尽管它应该是横向的。

编辑:

在我的文件 plist.info 中,我配置了 Supported Interface Orientation

编辑 2:

我的代码看起来像

[3]

【问题讨论】:

    标签: ios objective-c uiwebview uiinterfaceorientation


    【解决方案1】:

    我已经在一个新项目中测试了你的代码,它可以工作。 如果您正确配置了“支持的界面方向”条目,请检查您的 info.plist:


    还有另一种强制旋转的方法:

    #import <objc/message.h>
    

    然后当你想强制横向左方向使用这个:

     ((void (*)(UIDevice *, SEL, int))objc_msgSend)([UIDevice currentDevice],@selector(setOrientation:),UIInterfaceOrientationLandscapeLeft);
    

    【讨论】:

    • 嗨,我添加了我的 info.plist 的图像。我为 iPad 和 iPhone 配置了它
    • 好的,那么我假设您的“orientNr”方法没有按预期工作。
    • 替换 [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; with [[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
    • 我添加了两张图片。替换 ` [[UIDevice currentDevice] setValue:value forKey:@"orientation"];` 没有任何改变。方向仍然被忽略
    • 我已经在一个新项目中测试了代码并且它可以工作。我编辑了我的答案,向您建议另一种强制横向方向的方法
    猜你喜欢
    • 1970-01-01
    • 2017-03-31
    • 2014-06-24
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多