菜鸟教程小白 发表于 2022-12-11 19:35:53

ios - 应用程序在 ios 7 中更改方向时崩溃


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

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

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

<p>preferredInterfaceOrientationForPresentation 必须返回受支持的界面方向!</p>

<p>我正在使用此代码检查用户的方向</p>

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

<p>并使用此代码设置方向。 </p>

<pre><code> if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@&#34;8&#34;))
{
if(.orientation ==
UIDeviceOrientationPortrait ||.orientation ==
UIDeviceOrientationPortraitUpsideDown)
{

    NSLog(@&#34;Chnaging Device Orientation to stored orientation&#34;);
    NSNumber *value = ;
    [ setValue:value forKey:@&#34;orientation&#34;];
}
}
else
{
//Code For IPHONE 4
}
</code></pre>

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

<p>提前致谢!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我会尝试查找当前的设备方向,看看是否需要强制旋转设备。您可能还需要向您的 plist 文件添加纵向和横向功能,然后您不想自动旋转的任何屏幕都可以添加一个方法来调用。 </p>

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

<pre><code>-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if(self.shouldAllowRotation)
      return UIInterfaceOrientationMaskAll;
    else
      return UIInterfaceOrientationMaskPortrait;
}
</code></pre>

<p>然后在每个 View 中你可以调用</p>

<pre><code>[ setShouldAllowRotation:NO];
</code></pre>

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

<pre><code>- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}
</code></pre>

<p>但是,如果您将 ViewController 推送到导航 Controller 中的其他 ViewController 堆栈,则仅需要横向将无法正常工作。您应该以模态方式显示横向约束 ViewController 。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 应用程序在 ios 7 中更改方向时崩溃,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/43737226/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/43737226/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 应用程序在 ios 7 中更改方向时崩溃