在iPhone专用API上设置界面方向

问题描述:

任何人都可以确认[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];会让您的应用被Apple拒绝。在iPhone专用API上设置界面方向

有没有好的替代品可用?

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientation]是一个私有API,你可以发现签名是只读的(见here)。

如果你想设置的设备方向为纵向或其他方式,你应该覆盖shouldAutorotateToInterfaceOrientation:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return (interfaceOrientation == UIDeviceOrientationPortrait); 
} 
+2

的UIDevice是不是私人的API。 – thierryb 2010-03-05 13:23:22

+0

对不起,这是一个错误,我只是告诉setOrientation是私人的。 – 2010-03-05 13:36:04

您可以在您的应用程序的plist

Yes you'll get rejected添加UIInterfaceOrientation。

您可以更改框架并将变换应用到关键窗口,并更改状态栏方向(这是公开的)以模拟更改方向。

你可以试试这段代码。它完美对我的应用程序

UIApplication* application = [UIApplication sharedApplication]; 
if (application.statusBarOrientation != UIInterfaceOrientationPortrait) 
{ 
    AppDelegate* app = [[UIApplication sharedApplication] delegate]; 
    UIViewController *c = [[UIViewController alloc]init]; 
    [app.viewController presentModalViewController:c animated:NO]; 
    [app.viewController dismissModalViewControllerAnimated:NO]; 
    [c release]; 
} 

其中app.viewController是应用程序的RootViewController的