iPad:如何根据方向显示不同的屏幕(横向/纵向)

时间:2011-07-11 18:29:53

标签: xcode ipad orientation rotation viewcontroller

我有一个iPad应用程序,可用于所有四种视图模式(纵向上/下和横向左/右)。但在某个时刻,我有一个我只希望在风景模式中看到的视图。所以我在UIViewController中执行以下操作,触发操作以查看仅横向视图:

- (void) showProperty:(Property *) property {
    if ([self interfaceOrientation] == UIInterfaceOrientationLandscapeLeft || [self interfaceOrientation] == UIInterfaceOrientationLandscapeRight) {
        PropertyViewController *propertyView = [[PropertyViewController alloc] initWithNibName:@"PropertyViewController" bundle:[NSBundle mainBundle]];
        propertyView.property     = property;
        [self.navigationController pushViewController:propertyView animated:YES];
        [propertyView release];
        propertyView = nil;
    }
    else {
        RotateDeviceViewController *rotateView = [[RotateDeviceViewController alloc] initWithNibName:@"TabRotate" bundle: [NSBundle mainBundle]];
        rotateView.property = property;
        [self.navigationController pushViewController:rotateView animated:YES];
        [rotateView release];
        rotateView = nil;
    }
}

这很好用,因此当iPad以横向模式保持时显示所需的屏幕(PropertyViewController),如果没有,则显示RotateDeviceViewController,向用户显示他/她应该正确旋转设备的消息查看屏幕。

因此,当用户将他/她的设备旋转到横向模式时,我想向他们显示正确的视图(PropertyViewController)。所有这些都有效!

虽然在 RotateDeviceViewController 中出现问题..我有以下内容:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
        [self showProperty];
    return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}

- (void) showProperty {
    PropertyViewController *propertyView = [[PropertyViewController alloc] initWithNibName:@"PropertyViewController" bundle:[NSBundle mainBundle]];
    propertyView.property     = property;
    [self.navigationController pushViewController:propertyView animated:YES];
    [propertyView release];
}

因此,只要我将设备(查看 RotateDeviceViewController )旋转到横向模式,我就会向用户显示 PropertyViewController 。这有效...但是当 PropertyViewController 出现时,它显示我的布局旋转了90度。所以基本上它以纵向模式显示内容,而不是使用横向模式(这实际上是您持有设备的方式)..

我希望这是有道理的,有人可以告诉我是什么导致了这一点。

截图以使其更清晰:

1 个答案:

答案 0 :(得分:1)

此时

- (BOOL)shouldAutorotateToInterfaceOrientation:UIInterfaceOrientation)interfaceOrientation

您告诉视图控制器您支持哪些方向。设备实际上还没有旋转,因此视图控制器intefaceOrientation属性仍然是portrait,因此当它被推入堆栈时,它认为设备是portrait

pseudo code
shouldAutoRotate... // at this point self.interfaceOrientation == portrait
                    // you push your controller here so it loads when the property is 

我不确定这是否会运作良好,但我能看到你最早能推进的是

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation