UIViewController不会自动旋转

loo*_*grc 7 iphone uiviewcontroller

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

更新1:

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

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return YES; 
}
Run Code Online (Sandbox Code Playgroud)

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

怎么了?

更新2:

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

Dai*_*Lak 15

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

- (id)initWithAlbum:(PhotoAlbum *)theAlbum {
    if (self) {     
        self.photoAlbum = theAlbum;     
    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)

然后我改变了这个

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

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


nyb*_*bon 8

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

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

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

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


小智 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;
Run Code Online (Sandbox Code Playgroud)