2011-12-16 76 views
2

我有一个支持所有设备方向的iPhone应用程序,但我有一个特定的模态视图,我只想在横向上显示。显示模态视图,其方向不同于设备的方向

如果设备处于横向方向,它可以正常工作,但如果设备在纵向方向上保持不变,则不会显示我想要的方式。

由于设备以纵向方向保持,所以图像被裁剪并且左侧和右侧离开显示器的边缘。由于图像的高度仅与狭窄的尺寸一样高,因此顶部和底部显示父视图的背景。如果我然后旋转设备到横向方向,那么一切都很好。

在我的研究,我经常遇到shouldAutorotateToInterfaceOrientation,但我相信这是当设备在物理上旋转,只有被炒鱿鱼这并不适用于我的情况的事件。该设备在物理上保持纵向,但我想显示该视图,就好像它在横向上一样。

[UIApplication的sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]也提到但我从来没有看到有什么影响时,我一直在使用它尝试。

所以我想我的问题是,如果有一种简单的方法来强制视图的初始显示方向不同于设备的物理方向,以及如何执行此操作。

我使用下面的逻辑来模态地显示我的观点:

TitleViewController *titleController = [[TitleViewController alloc] initWithNibName:@"TitleViewController" bundle:[NSBundle mainBundle]]; 
    titleController.delegate = self; 
    titleController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
    [self presentModalViewController:titleController animated:YES]; 
    [titleController release]; 

我想在风景方向始终显示模式的看法,即使设备处于纵向举行。

UPDATE

找到一个可行的解决方案:

if (self.interfaceOrientation == UIInterfaceOrientationPortrait) 
    { 
     self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90)); 
     self.view.bounds = CGRectMake(0.0, 0.0, 480, 320); 
    } 

然而,随着取向UIInterfaceOrientationPortraitUpsideDown处理时,最初显示与正确方向的视图,但然后转变回一个肖像方向与剪裁的两侧,并显示图像上方和下方的背景。

我已经尝试调整角度为270,-90和其他值,但总是在面向UIInterfaceOrientationPortraitUpsideDown方向时恢复为纵向。

为什么?

回答

2

这种方法做了我所需要的一切:

if (self.interfaceOrientation == UIInterfaceOrientationPortrait) 
    { 
     self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90)); 
     self.view.bounds = CGRectMake(0.0, 0.0, 480, 320); 
    } 

    TitleViewController *titleController = [[TitleViewController alloc] initWithNibName:@"TitleViewController" bundle:[NSBundle mainBundle]]; 
    titleController.delegate = self; 
    titleController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
    [self presentModalViewController:titleController animated:YES]; 
    [titleController release]; 

旋转之前的方向呈现我的模态观点。

而我刚刚删除了支持UIInterfaceOrientationPortraitUpsideDown因为不需要这个方向。

0

解决方案1:要从初始肖像模式更改启动方向,您需要编辑您的info.plist文件。为“初始界面方向”添加条目并设置所需的值。

Here是哪里可以找到info.plist文件的截图。(如果该链接不起作用,请留言)


解决方案2:你也可以在你的导航控制器作为一个子视图到窗口和方向的通知尝试这只会被发送到第一子窗口中的子视图。

//.m file 
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
    // Hide status bar 
    application.statusBarHidden = YES; 
     // --- Add the view controller's view to the window and display. 
    [window addSubview:viewController.view]; 
    [viewController.view addSubview:navigationController.view]; 
    [window makeKeyAndVisible]; 
    return YES; 
} 
+0

问题是,在最终请求并显示模态视图之前,方向可能会改变很多次并处于任何方向。 – GRW 2011-12-16 20:15:18

1

你试过:

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight]; 

而且当你要关闭它改变为纵向

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait]; 
+0

这似乎在一定程度上工作,但是它确实产生似乎工作(有点)警告 – GRW 2011-12-16 21:02:02

相关问题