簡體   English   中英

應用程序在ios 7中更改方向時崩潰

[英]App crashing on changing orientation in ios 7

我正在開發一款基本上在橫向模式下運行的游戲。 它具有更改個人資料照片的選項。 配置文件圖片選項將打開圖像選擇器,該圖像選擇器用於顯示在配置文件圖片圖像視圖中。 我的問題是

打開圖像選擇器后,應用程序會自動將方向更改為縱向。

為了解決這個問題,我嘗試捕獲用戶單擊上傳圖像按鈕時的方向,並在選擇圖像后強制將設備方向更改為上一個方向。 在iPhone 5和更高版本上,一切正常。 但是,當用戶從選擇器視圖中選擇圖像時,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上正常運行

提前致謝!

我將嘗試查找當前設備的方向,並查看是否需要強制旋轉設備。 您可能還需要在plist文件中添加縱向和橫向功能,然后您不想自動旋轉的任何屏幕都可以添加一種調用方法。

例如,您可以在應用程序委托中添加帶有可訪問變量shouldAllowRotation的此類方法:

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

然后您可以在每個視圖中調用

[[AppDelegate instance] setShouldAllowRotation:NO];

取決於是否應該旋轉。 調用選擇器時,只需調用應用程序委托並使其自動旋轉,這將使您能夠

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

但是,如果將視圖控制器推到導航控制器中的其他視圖控制器的堆棧中,則僅要求橫向視圖不能很好地工作。 您應該以模態顯示橫向約束視圖控制器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM