iPhone SDK:方向(横向和纵向视图)

问题描述:

好吧,我有我的正常应用程序,这是在肖像模式。我可以强迫我的应用程序去横向模式视图(使用navigationcontroller和ViewController)是这样的:iPhone SDK:方向(横向和纵向视图)

- (void)viewWillAppear:(BOOL)animated { 
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight]; 
} 

但是当我回到主菜单(tableview中),它会直接回到画像。我试试这个代码:

- (void)viewWillAppear:(BOOL)animated { 
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait]; 
} 

但是,这并不工作..

任何想法?

查看UIViewController类中的函数shouldAutorotateToInterfaceOrientation:。如果您的UIView支持方向,则此函数返回YES。如果您仅将YES返回到横向,则iPhone会自动放入该方向。

下面的代码应该这样做。把它放在UIViewController中,该UIViewController控制你想放在横向模式下的视图。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationLandscape); 
} 

您需要编辑Info.plist文件以添加具有适当值(UIInterfaceOrientationLandscapeRight或UIInterfaceOrientationLandscapeLeft)的UIInterfaceOrientation键。

+0

是,仅仅回到一个画像表主屏幕?因为我想要一切肖像,只有一个景观。我可以看到它的风景,但是当我回去时,我必须旋转手机才能将它恢复成肖像。我不希望用户有其他的风景。 – Domness 2009-01-10 16:26:52

为了得到这个工作,我已将此添加到RootViewController的:

- (void)viewWillAppear:(BOOL)animated { 
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait]; 

} 

这似乎是现在的工作。

+0

这算作使用私人API吗?方向被记录为只读属性。 – lawrence 2009-05-16 19:29:41

+0

这一切都很好,通过苹果:) – Domness 2009-05-17 01:18:22

下面是我在做什么这样做:

首先,把这个定义在文件的顶部,就在你#imports:

#define degreesToRadian(x) (M_PI * (x)/180.0) 

然后,在viewWillAppear中:方法

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];  
if (self.interfaceOrientation == UIInterfaceOrientationPortrait) { 
    self.view.transform = CGAffineTransformIdentity; 
    self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90)); 
    self.view.bounds = CGRectMake(0.0, 0.0, 480, 320); 
} 

,如果你想要的是动画,那么你可以用整个事情在动画块,像这样:

[UIView beginAnimations:@"View Flip" context:nil]; 
[UIView setAnimationDuration:1.25]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];  
if (self.interfaceOrientation == UIInterfaceOrientationPortrait) { 
    self.view.transform = CGAffineTransformIdentity; 
    self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90)); 
    self.view.bounds = CGRectMake(0.0, 0.0, 480, 320); 
} 
[UIView commitAnimations]; 

然后,在您的肖像模式控制器中,您可以执行相反的操作 - 检查当前是否在横向,如果是,则将其旋转回肖像。

您不能使用此私有API。

有一个解决方案来做到这一点:它使用视图控制器并将其视图添加到窗口。那么在那个控制器中,你会在shouldAutorotate ...方法中强制横向。它工作正常,但要确保你的项目需要使用它,因为强迫用户转动他的iPhone并不是很聪明。顺便说一句,如果你需要的话,这里是一个示例代码。

http://www.geckogeek.fr/iphone-forcer-le-mode-landscape-ou-portrait-en-cours-dexecution.html