OGeek|极客世界-中国程序员成长平台

标题: ios - 应用程序在 ios 7 中更改方向时崩溃 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:35
标题: ios - 应用程序在 ios 7 中更改方向时崩溃

我正在开发一款基本上以横向模式运行的游戏。 它有一个更改个人资料图片的选项。 个人资料图片选项打开用于在个人资料图片 ImageView 上显示的图像选择器。 我的问题是

当图像选择器打开时,应用会自动将方向更改为纵向。

为了解决这个问题,我尝试在用户单击上传图像按钮时捕捉用户的方向,并在选择图像后,强制将设备方向更改为之前的方向。 在 iPhone 5 及更高版本上一切正常。但是当用户从选取器 View 中选择图像时,iphone 4(在 ios7 上运行)崩溃。我收到以下错误 -

preferredInterfaceOrientationForPresentation 必须返回受支持的界面方向!

我正在使用此代码检查用户的方向

-(void)checkCurrentDeviceOrientation
{
if([UIDevice currentDevice].orientation == 
UIDeviceOrientationLandscapeRight || [UIDevice 
currentDevice].orientation == UIDeviceOrientationLandscapeLeft )
{
self.currentOrientation = [UIDevice currentDevice].orientation;
}
else
{
self.currentOrientation = UIDeviceOrientationLandscapeRight;
}
}

并使用此代码设置方向。

 if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8"))
 {
 if([UIDevice currentDevice].orientation == 
UIDeviceOrientationPortrait ||[UIDevice currentDevice].orientation == 
UIDeviceOrientationPortraitUpsideDown  )
 {

    NSLog(@"Chnaging Device Orientation to stored orientation");
    NSNumber *value = [NSNumber numberWithInt:self.currentOrientation];
    [[UIDevice currentDevice] setValue:value forKey"orientation"];
}
}
else
{
//Code For IPHONE 4
}

希望在 ios8 及更高版本上一切正常,但在运行 ios 7.0.1 的 iphone 4 上却不行

提前致谢!



Best Answer-推荐答案


我会尝试查找当前的设备方向,看看是否需要强制旋转设备。您可能还需要向您的 plist 文件添加纵向和横向功能,然后您不想自动旋转的任何屏幕都可以添加一个方法来调用。

例如,您可以在应用程序委托(delegate)中添加这样的方法,并使用可访问变量 shouldAllowRotation:

-(UIInterfaceOrientationMask)applicationUIApplication *)application supportedInterfaceOrientationsForWindowUIWindow *)window
{
    if(self.shouldAllowRotation)
        return UIInterfaceOrientationMaskAll;
    else
        return UIInterfaceOrientationMaskPortrait;
}

然后在每个 View 中你可以调用

[[AppDelegate instance] setShouldAllowRotation:NO];

取决于它是否应该旋转。当调用采摘器时,只需调用应用程序委托(delegate)并自动旋转,这将使您获得

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

但是,如果您将 View Controller 推送到导航 Controller 中的其他 View Controller 堆栈,则仅需要横向将无法正常工作。您应该以模态方式显示横向约束 View Controller 。

关于ios - 应用程序在 ios 7 中更改方向时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43737226/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4