2016-01-12 58 views
0

我曾嘗試使用下面的代碼如何在iOS中返回前臺時更改設備方向?


    NSNumber *value = [NSNumber  numberWithInt:UIInterfaceOrientationLandscapeLeft]; 
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 

從縱向方向更改爲子視圖控制器的風景,但是當我去到後臺,並返回到前臺,然後我按下按鈕轉到主頁,然後需要改變使用下面的代碼


    NSNumber *value = [NSNumber  numberWithInt:UIInterfaceOrientationPortrait]; 

    [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    if ([ConfigObjC currentPage] && [[ConfigObjC currentPage] isEqualToString:@"SubViewController"]) 
    { 
     return UIInterfaceOrientationMaskLandscapeLeft; 
    } 
    else{ 
     return UIInterfaceOrientationMaskPortrait; 
    } 
} 

,但它不能正常工作從橫向到縱向。

我已經成功地改變從風景到肖像的方向,當我不去背景。通過編碼是

<key>UISupportedInterfaceOrientations</key> 
<array> 
<string>UIInterfaceOrientationLandscapeLeft</string> 
<string>UIInterfaceOrientationLandscapeRight</string> 
</array> 

另一種方法:我怎樣才能解決這個問題,非常感謝

回答

0

如果您想將您在橫向模式下的窗口,你可以修改Xcode的信息,只支持橫向,這樣改變方向的UIWindow,像這樣:

UIApplication *application=[UIApplication sharedApplication]; 
[application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]; 
application.keyWindow.transform=CGAffineTransformMakeRotation(M_PI); 

使用上面這種方式需要設置ShouldAutoRotateNO

- (BOOL)shouldAutorotate{ 
    return NO; 
} 

此外,如果只有在橫向模式下顯示多個視圖,您可以使用屬性transform旋轉這些觀點,是這樣的:

//rotate screen only with view 
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]; 

self.view.transform = CGAffineTransformMakeRotation(M_PI/2); 

希望能幫助你!

+0

我已經做了使用下面的代碼的方向NSNumber * value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft]; [[UIDevice currentDevice] setValue:value forKey:@「orientation」];但是當我轉到背景並返回到前景時,我嘗試使用NSNumber * value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait]將方向更改爲Portrait。 [[UIDevice currentDevice] setValue:value forKey:@「orientation」];它不起作用 – vincent

+0

UIDeviceOrientation是隻讀的,UIInterfaceOrientation是readwrite。您可以使用UIDeviceOrientation註冊通知,然後旋轉屏幕以及查看 – SeraZheng