如何在tabbarcontroller中旋转一个视图?

时间:2013-01-22 20:35:24

标签: ios objective-c rotation

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

我正在使用iOS6编写一个标签式应用程序,我想让其中一个视图控制器旋转,其余部分保持不变。我知道我需要使用supportedInterfaceOrientations,但我不知道如何或在何处使用它,而且我无法破译有关它的Apple文档。 UITabBarController被设置为AppDelegate中的根视图控制器,如果这是相关的。

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

这个问题让我死了一个星期。我有完全相同的情况,只希望能够旋转一个单一的视图。您可以通过注册方向通知来选择在标签式应用中解决问题的方法。在TabController(以及任何其他superViews)和要旋转的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中放置一个bool,因为在视图实际出现之前你无法进行切换。

现在,如果您在横向模式下更改的视图不是选项卡式视图控制器,那么处理该视图的最佳方法是在该视图中允许AutoRotation,然后在willAnimate中,您只需忽略VC: / p>

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

如果它是标签视图,那么只需在该视图中注册orientationchanges。但是,请确保在离开视图时删除orientationChange通知。如果你不这样做,那么你也将从其他viewControllers继续观察。

当你离开去另一个标签时,你需要实现tabBarController委托,并确保你删除观察者:

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

}

答案 1 :(得分:1)

在UITabBarController子类中实现supportedInterfaceOrientations。这是一个示例,其中第一个视图控制器不会自动旋转肖像:

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

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

答案 2 :(得分:-1)

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

[self.view setTransform:CGAffineTransformMakeRotation(-M_PI / 2)];
相关问题