支持多个方向的最简单方法?当应用程序在Landscape中时,如何加载自定义NIB?

问题描述:

我有一个应用程序,我想支持多个方向。我有两个我想使用的.xib文件,myViewController.xib和 myViewControllerLandscape.xib。 myViewController.xib 存在于project / Resources中, myViewControllerLandscape.xib 存在于根项目目录中。

I have an application in which I would like to support multiple orientations. I have two .xib files that I want to use, myViewController.xib and myViewControllerLandscape.xib. myViewController.xib exists in project/Resources and myViewControllerLandscape.xib exists in the root project directory.

我想要做的是使用单独的NIB (myViewControllerLandscape.xib)进行轮换。我尝试在中检测旋转.viewDidLoad l ike this:

What I want to do is use a separate NIB (myViewControllerLandscape.xib) for my rotations. I try detecting rotation in viewDidLoad like this:

if((self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight))
 {
  NSLog(@"Landscape detected!");
  [self initWithNibName:@"myViewControllerLandscape" bundle:nil];

 }

但是我可以在gdb中看到这个没有被执行当应用程序以横向启动设备时。 NSLog消息不会触发。为什么是这样?我做错了什么?

But I can see in gdb that this isn't executed when the app is started with the device in landscape. The NSLog message doesn't fire. Why is this? What have I done wrong?

另外,如果我在 initWithNibName 函数调用c> viewDidLoad 方法,未加载该nib,并继续使用 myViewController.xib 文件。我的电话有什么问题?我应该指定一个包吗?

Also, if I explicitly put the initWithNibName function call in the viewDidLoad method, that nib is not loaded, and it continues with the myViewController.xib file. What's wrong with my call? Should I specify a bundle?

谢谢!

你有加载一个视图,然后检查方向并在需要时加载另一个视图。你检查中的方向shouldAutorotateToInterfaceOrientation:如果要旋转,则返回yes。

You have to load one view, then check orientation and load another if needed. You check orientation in shouldAutorotateToInterfaceOrientation: returning yes if you want to rotate.

我使用导航控制器来管理过渡。如果我有纵向视图并且设备旋转,我会推动横向视图,然后在返回纵向时弹出横向视图。

I use a navigation controller to manage the transition. If I have the portrait view up and the device rotates, I push the landscape view and then pop the landscape view when it return to portrait.

编辑:


我为
中的所有方向返回YES shouldAutorotateToInterfaceOrientation:
但是当app
启动时会调用它吗?你是否在这个函数的
中推动你的视图?

I return YES for all orientations in shouldAutorotateToInterfaceOrientation: but will this be called when the app launches? Do you push your view inside of this function?

方向常量不是你查询的全局变量而是部分的消息由系统发送控制器。因此,在视图控制器加载之前,您无法轻松检测方向。相反,您硬连线应用程序以特定方向(通常是人像)开始,然后立即旋转。 (参见移动Safari。它始终以纵向开始,然后旋转到横向。)

The orientation constants are not globals you query but rather part of the messages sent the controller by the system. As such, you cannot easily detect orientation before a view controller loads. Instead, you hardwire the app to start in a particular orientation (usually portrait) and then immediately rotate. (See mobile Safari. It always starts in portrait and then rotates to landscape.)

这是我用来换出纵向和横向视图的两种方法。

These are the two methods I used to swap out my portrait and landscape views.

所有三个视图控制器都有这种方法:

All three view controllers have this method:

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

肖像有这样的:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight) {
        [self.nav pushViewController:rightLVC animated:NO];
    }
    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
        [self.nav pushViewController:leftLVC animated:NO];
    }
}

每个横向控制器都有:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    if (toInterfaceOrientation==UIInterfaceOrientationPortrait) {
        [self.nav popViewControllerAnimated:NO];
    }

应用程序以纵向开始。如果设备的方向是横向的,则会推动适当的景观。当设备旋转回纵向时,它会弹出景观。对于用户来说,它看起来像是相同的视图,为不同的方向重新组织自己。

The app starts in portrait. If the orientation of the device is landscape, it pushes the appropriate landscapes. When the device rotates back to portrait, it pops the landscape. To the user it looks like the same view reorganizing itself for a different orientation.