2009-02-11 43 views
2

我想在出現特定視圖時更改設備方向。我獲得了有關在方向更改時要採取的操作的很多信息,但我沒有得到如何以編程方式更改設備方向本身。請注意,我的其他視圖必須保持默認方向。更改特定UIViewController子類的設備方向

回答

0
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait]; 

請注意,這不是API文檔中的UIDevice框架的一部分,因此蘋果可能會拒絕使用這種應用程序。

+0

這可行,但@micco注意到應用程序可能會被拒絕。 [@got nate](http://stackoverflow.com/users/145186/)證實了這一點:'感謝您將[app name]提交給App Store。不幸的是,它不能被添加到App Store,因爲它使用的是私有API。禁止使用iPhone開發者計劃許可協議第3.3.1節中概述的非公開API: 「3.3.1應用程序只能按照Apple規定的方式使用文檔化的API,並且不得使用或調用任何私人蜜蜂。」 包含在您的應用程序中的非公共API是setOrientation :.' – 2011-10-01 23:09:26

0

您可以使用CGAffineTransforms而不是私有API來更改視圖的方向。您必須更改UIStatusBarOrientation方向以匹配新的方向並找到一個角度來旋轉視圖:

例如,如果您的視圖處於橫向方向並且您希望縱向回顯。

CGAffineTransform transform = CGAffineTransformIdentity; 

switch ([[UIApplication sharedApplication] statusBarOrientation]) { 

    case UIInterfaceOrientationLandscapeLeft: 
     transform = CGAffineTransformMakeRotation(M_PI_2); 
     break; 

    case UIInterfaceOrientationLandscapeRight: 
     transform = CGAffineTransformMakeRotation(-M_PI_2); 
     break; 

    default: 
     break; 
} 

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait]; 

[UIView animateWithDuration:0.2f animations:^ { 

    [view setTransform:transform]; 
}]; 

[view setFrame:CGRectMake(0, 0, 320, 480)]; 
[view setNeedsLayout];