手动设置界面方向

问题描述:

有没有简单的方法来手动设置界面方向?我需要将界面设置为纵向,即使装载期间设备方向可能处于横向。有点想远离CGAffineTransforms。手动设置界面方向

一种方法,我知道这对我的作品(并且是一个黑客位,并可以改变到你想要的方向之前显示一个方向)是:

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    UIApplication* application = [UIApplication sharedApplication]; 

    if (application.statusBarOrientation != UIInterfaceOrientationPortrait) 
    { 
     UIViewController *c = [[UIViewController alloc]init]; 
     [self presentModalViewController:c animated:NO]; 
     [self dismissModalViewControllerAnimated:NO]; 
     [c release]; 
    } 
} 
+0

这实际上对我描述的情况非常有效。但由于某些原因,不适合尝试从纵向模式强制横向(将UIInterfaceOrientationPortrait更改为UIInterfaceOrientationLandscapeLeft)。想法? –

+0

实际工作的肮脏的黑客。 – beefon

+0

谢谢!伟大的代码行=) – CReaTuS

覆盖它来控制方向,直到装...

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
+0

方向属性为只读 –

+0

是的,您是对的。我的错。我更新了我的答案。您可以考虑重写shouldAutorotateToInterfaceOrientation并根据需要维护方向。 – Saran

首先,设置你的应用程序和观点,只支持人像,然后使用从我的重构库中提取的这一类方法,es_ios_utils

@interface UIViewController(ESUtils) 
    // Forces without using a private api. 
    -(void)forcePortrait; 
@end 

@implementation UIViewController(ESUtils) 

-(void)forcePortrait 
{ 
    //force portrait orientation without private methods. 
    UIViewController *c = [[UIViewController alloc] init]; 
    [self presentModalViewController:c animated:NO]; 
    [self dismissModalViewControllerAnimated:NO]; 
    [c release]; 
} 

@end 

该视图在帧完成前关闭,将不会显示。

+0

我可能从Shane或他的消息来源得到了这个。他有点更完整,我的建议使用一个类别来重用。 –

+0

谢谢!伟大的代码行=) – CReaTuS

+0

哇,你能解释更多关于它是如何工作的? – OMGPOP