1

我觉得我的问题在iOS6 中得到了回答:supportedInterfaceOrientations not working (is invoked but the interface still rotates),但我不够先进,无法理解如何实现响应。

我正在使用 iOS6 编写一个选项卡式应用程序,我希望其中一个视图控制器旋转,其余的保持不动。我知道我需要使用supportedInterfaceOrientations,但我不知道如何或在哪里使用它,而且我无法破译有关它的 Apple 文档。被UITabBarController设置为 中的根视图控制器AppDelegate,如果相关的话。

在此先感谢您的帮助。

4

3 回答 3

2

这个问题让我死了一个星期。我有完全相同的情况,也只想能够旋转一个视图。在选项卡式应用程序中绕过它的方法是注册方向通知。在 TabController(和任何其他超级视图)和要旋转的 VC 中为 shouldAutoRotate 返回 NO。然后在 viewWillAppear 中注册通知:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)name:UIDeviceOrientationDidChangeNotification
          object:[UIDevice currentDevice]];

被调用的函数将如下所示:

- (void) orientationChanged:(NSNotification *)note
{
    UIDevice * device = note.object;
    switch(device.orientation)
    {
        case UIDeviceOrientationPortrait:
            break;

        case UIDeviceOrientationPortraitUpsideDown:
            break;
        case UIDeviceOrientationLandscapeLeft:
            if(viewHasAppeared==true){
                [self performSegueWithIdentifier:@"firstToLandscape" sender:self];}
            break;
        case UIDeviceOrientationLandscapeRight:
            if(viewHasAppeared==true){
                [self performSegueWithIdentifier:@"firstToLandscape" sender:self];}
            break;
        default:
            break;
    };
}

- 在 ViewDidAppear 中放置一个布尔值,因为在视图实际出现之前您无法继续。

现在,如果您在横向模式下更改为的视图不是选项卡式视图控制器,那么处理该问题的最佳方法是只允许在该视图中进行 AutoRotation,然后在 willAnimate 中您只需像这样关闭 VC:

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
    {
       [self dismissViewControllerAnimated:YES completion:nil];     
    }
}

如果它是选项卡式视图,那么也只需在该视图中注册方向更改。但是,请确保在离开视图时删除orientationChange 通知。如果你不这样做,那么你也会从其他视图控制器中切换到横向。

当你离开去另一个选项卡时,你需要实现 tabBarController 委托,并确保你像这样删除观察者:

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if(![NSStringFromClass([viewController class])isEqualToString:@"FirstViewController"])
    {
            [[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];             
    }

}
于 2013-01-22T21:07:56.683 回答
1

supportedInterfaceOrientations在您的 UITabBarController 子类中实现。这是一个示例,其中第一个 View Controller 不会自动旋转出纵向:

- (NSUInteger)supportedInterfaceOrientations {
    if ( self.selectedIndex == 0 )
        return UIInterfaceOrientationMaskPortrait ;
    else
        return UIInterfaceOrientationMaskAll ;
}

但是,如果您切换到 View Controller #2(索引 1),旋转到横向,然后切换回 View Controller #1,您将看到 View Controller #1 以横向显示。我正在寻找解决方案。

于 2013-01-22T20:54:49.540 回答
-1

实现此目的的一种简单方法是正确加载视图并在 viewDidLoad: 方法结束时使用转换旋转整个 UIView

[self.view setTransform:CGAffineTransformMakeRotation(-M_PI / 2)];
于 2013-01-22T20:52:23.377 回答