UIViewController不会自动旋转

时间:2010-03-10 11:29:05

标签: iphone uiviewcontroller

正如标题所说。无论如何,我的UIViewController都不会旋转。当它加载时,应调用了AutorotateToInterfaceOrientation,但之后它就没有了。

更新1:

这是一个非常奇怪的问题。至少对于我来说。我会尝试解释一切。

这是一款基于导航的应用。每个控制器都有

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return YES; 
}

Xcontroller是Acontroller的孩子,它不会自动旋转。如果Xcontroller成为Bcontroller的孩子,那么它将自动旋转。所以Acontroller有问题。但Acontroller与Bcontroller完全相同(除了数据)。

什么是错的?

更新2:

我决定重新创建Acontroller。它奏效了。我相信我错过了一些愚蠢的东西。

9 个答案:

答案 0 :(得分:15)

我不确定这与你的情况是否相同。但我经历了同样的事情。 shouldAutorotateToInterfaceOrientation仅在开始时被调用一次。 通过分开代码进行一些严格的调试后,我发现原因在于我的重写方法。 我之前有这个:

- (id)initWithAlbum:(PhotoAlbum *)theAlbum {
    if (self) {     
        self.photoAlbum = theAlbum;     
    }
    return self;
}

然后我改为

- (id)initWithAlbum:(PhotoAlbum *)theAlbum {
    if (self = [super init]) {      
        self.photoAlbum = theAlbum;     
    }
    return self;
}

注意:唯一的区别是我添加[super init]来调用父init。 在此更改之后,旋转效果很好,每次旋转屏幕时都会调用shouldAutorotateToInterfaceOrientation。 希望这有帮助。

答案 1 :(得分:8)

视图控制器无法旋转可能有多种原因。

请参阅Apple关于此问题的官方问答:

  

为什么我的UIViewController不会随设备一起旋转?

     

http://developer.apple.com/library/ios/#qa/qa2010/qa1688.html

答案 2 :(得分:5)

Apple Q& A详细解决了这个问题。

为什么我的UIViewController不会随设备一起旋转?

http://developer.apple.com/library/ios/#qa/qa1688/_index.html

如果将viewcontroller.view添加到uiwindow,则应将此viewcontroller设置为rootviewcontroller。

[self.window addSubview: mainViewcontroller.view];
self.window.rootViewController=mainViewcontroller;

答案 3 :(得分:3)

另外,请确保没有旋转锁定。我花了一个小时试图找出为什么我的观点停止了旋转。 shouldAutorotateToInterfaceOrientation仅在启动时被调用,并且当游戏中心的排行榜/成就被呈现时。

答案 4 :(得分:3)

我有同样的问题 - 原因是,它是我的第一个UIViewController,我在ApplicationDelegate中动态创建,将它的View添加到我的UIWindow并立即发布它。

这当然不正确,因为我刚刚添加了UIViewController的UIView(保留它)而不是释放整个控制器。

你应该在你的ApplicationDelegate中添加你的第一个UIViewController作为实例变量,并在你的ApplicationDelegate的dealloc方法中释放它。

答案 5 :(得分:1)

在我的例子中,ViewController位于一个NavigationController中,该控件由接收方向更改的“父”viewControlled使用。

我在这位家长身上所做的是:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{

    if(_navigationController){
        return  [_navigationController.topViewController shouldAutorotateToInterfaceOrientation: toInterfaceOrientation];
    }

    return toInterfaceOrientation == UIInterfaceOrientationPortrait;
}

这样,您可以根据当前可见的控制器实现自己的方向更改逻辑。

答案 6 :(得分:0)

  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {return YES; } 如果您使用上述方法,您可以在没有任何错误的情况下多次调用。

答案 7 :(得分:0)

我认为这里没有奇怪的行为,只有一个是正确的。无需多次调用即可确定设备是否应旋转到某个方向。

此方法只询问设备是否应旋转到某个方向。 如果要处理方向更改,则应从UIDeviceDidChangeOrientationNotification注册通知并覆盖以下方法:

- (void)orientationChanged:(NSNotification *)notification
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
    !isShowingLandscapeView)
    {
        [self presentModalViewController:self.landscapeViewController
                            animated:YES];
        isShowingLandscapeView = YES;
    }
    else if (deviceOrientation == UIDeviceOrientationPortrait &&
             isShowingLandscapeView)
    {
        [self dismissModalViewControllerAnimated:YES];
        isShowingLandscapeView = NO;
    }
}

查看更多here

答案 8 :(得分:0)

我遇到了同样的问题,但是在应用程序的UIWindow中添加了两个视图控制器。原因 视图控制器的UIView属性嵌入在UIWindow中,但与其他视图控制器一起

来自Apple Technical Q& A

http://developer.apple.com/library/ios/#qa/qa1688/_index.html