UIViewController无法自动旋转。

7

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

更新1:

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

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

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

Xcontroller是Acontroller的子级,它不会自动旋转。如果Xcontroller成为Bcontroller的子级,则将自动旋转。因此,Acontroller存在问题。但Acontroller(除数据外)与Bcontroller相同。

出了什么问题?

更新2:

我决定重新创建Acontroller。然后它就可以了。我相信我错过了某些愚蠢的东西。


你是如何实现 -shouldAutorotateToInterfaceOrientation: 的? - kennytm
  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return YES; }
如果这是您的意思的话 :)。
- looneygrc
9个回答

15

我不确定是否跟你的情况一样,但我也遇到过类似的问题。 shouldAutorotateToInterfaceOrientation 方法只被调用了一次。 通过仔细调试和分析代码后,我发现问题出在我的覆盖了 init 方法上。 我之前写的是:

- (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]以调用父类的初始化方法。 经过这个改变,屏幕旋转能够正常工作,并且每次旋转屏幕时都会调用shouldAutorotateToInterfaceOrientation方法。 希望这能帮到你。

1
嘿DaiLak,你救了我的命。我和你一样遇到了同样的问题,我已经苦苦挣扎了几个小时了。谢谢。 - Cullen SUN

8

5

苹果问答提供了该问题的详细解决方案。

为什么我的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

另外,请确保您的旋转锁定未开启。我花了一个小时才弄清楚为什么我的视图停止旋转。在启动时和展示Game Center排行榜/成就时,只有一次调用了shouldAutorotateToInterfaceOrientation。


3

我曾经遇到同样的问题 - 原因是我在ApplicationDelegate中动态创建了我的第一个UIViewController,并将其视图添加到UIWindow中,然后立即释放了它。

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

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


1
在我的情况下,ViewController 在 NavigationController 中,该 NavigationController 被“父”视图控制器使用,该父视图控制器接收方向更改。
我在这个父级中所做的是:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{

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

    return toInterfaceOrientation == UIInterfaceOrientationPortrait;
}

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


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;
    }
}

请点击此处以查看更多。


谢谢你的回答。但我不明白为什么在我的其他控制器中,shouldAutorotateToInterfaceOrientation就足以旋转它们。而对于这个特定的控制器来说,这还不够。此外,在我的其他控制器中,每次我旋转设备时,shouldAutorotateToInterfaceOrientation都会被调用。 - looneygrc
换句话说,我的控制器应该在我旋转设备时调用shouldAutorotateToInterfaceOrientation方法来检查是否支持该方向。可是我的“有问题”的控制器只在加载时调用它,而不是在旋转设备时调用。 - looneygrc
当设备旋转时,它通过查询“shouldAutorotateToInterfaceOrientation:”询问控制器是否支持旋转。如果答案是肯定的,那么它将调用willRotateToInterfaceDuration:duration:来查看是否需要进行其他修改。 - Hoang Pham
我明白。但在我的情况下,它只在控制器加载时询问。如果我旋转设备,它就不会询问了。没关系,我会重新创建这个控制器,看看是否能解决问题。 - looneygrc
就像我告诉你的那样,iPhone操作系统没有理由调用此函数超过1次。因此,您应该寻找其他地方来处理设备方向。 - Hoang Pham
好的..明白了。但是为什么,在我的情况下,它不会“自动”旋转呢? - looneygrc

0
  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return YES; } 如果您使用上述方法,您可以随意多次调用而不会出现任何错误。

我正在使用这个方法。它只在控制器加载时被调用。 - looneygrc

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接