2

我有一个标签栏应用程序,其中包含 2 个标签中的导航视图。我希望 1 个导航控制器中的 1 个视图允许横向视图,但由于选项卡栏中的导航栏限制,我现在必须允许我的应用程序中每个视图的横向视图,以使倾斜消息传递给我的应用程序不想要。

我可能认为,在不应该变成横向的视图上,可能有两种方法:防止视图更改,例如在设备变为横向时调用 setOrientation:UIDeviceOrientationPortrait 或给出视图不会改变的错觉,例如呈现旋转视图上的模态纵向视图

有人有任何想法或经验想分享吗?这里最好的方法是什么?(我现在不想为每个视图设计一个横向视图,以便我可以为 1 个视图显示纵向和横向视图)

4

1 回答 1

2

我最近不得不处理同样的问题,我的解决方案如下:

在您希望能够旋转的视图的 UIViewController 中,为UIDeviceOrientationDidChangeNotification

-(void)viewDidLoad {


[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(didRotateFromInterfaceOrientation)
                                             name:@"UIDeviceOrientationDidChangeNotification" object:nil];

}

那么当然你需要实现你的didRotateFromInterfaceOrientation方法。

在该方法中,您可以使用

 UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

我接下来要做的是根据方向评估我想要显示的视图

switch (orientation) {
    case UIDeviceOrientationLandscapeLeft:
        NSLog(@"UIDeviceOrientationLandscapeLeft");
        [self presentModalViewController:LandscapeView animated:YES];

        break;
    case UIDeviceOrientationLandscapeRight:
        NSLog(@"UIDeviceOrientationLandscapeRight");

        [self presentModalViewController:LandscapeView animated:YES];

        break;
    case UIDeviceOrientationPortraitUpsideDown:
        NSLog(@"UIDeviceOrientationPortraitUpsideDown");
        [LandScapeview dismissModalViewControllerAnimated:YES];

        break;
    case UIDeviceOrientationPortrait:
        NSLog(@"UIDeviceOrientationPortrait");
        [LandscapeView dismissModalViewControllerAnimated:YES];
        break;

    case UIDeviceOrientationFaceUp:
        NSLog(@"UIDeviceOrientationFaceUp");

        break;

    case UIDeviceOrientationFaceDown:
        NSLog(@"UIDeviceOrientationFaceDown");
        break;

    default:
        break;
    }
}

我希望我能帮上一点忙。

于 2010-05-06T07:26:06.530 回答