UIViewController没有随着设备一起旋转的原因

对于iPhone app,UIViewController类提供了基本的视图管理模式。当设备改变方向的时候view controller的视图会自动随之旋转的。如果视图和子视图的autoresizing属性设置是对的,这时候视图又没有随着设备一起旋转,可能是以下的原因:

1.view controller没有完成代理方法

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;

也要实现了shouldAutorotateToInterfaceOrientation方法,同时shouldAutorotateToInterfaceOrientation方法要返回YES来支持所有的旋转方向


2.view controller的UIView属性嵌入在UIWindow中,并非是一个附加的view controller

你可能会发现shouldAutorotateToInterfaceOrientation方法只会在view controller启动的时候被调用,不会因为设置的旋转而在被调用。这是因为view controller束缚着他们所管理的视图,view controller是用来处理时间的响应链的一部分。view controller是从UIResponder继承而来,同时他被插入到他所管理的视图和他的superview之间。因此,通常的做法是在你的app中有一个主view controller来作为响应链的一部分。通常来说会添加一个主view controller,例如UINavigationController, UITabBarController或者UIViewControllerUIWindow

例如

[myWindow addSubview:primaryViewController.view]; 

如果你添加了另外一个view controller的UIView属性到UIWindow(anotherController和主view controller在同一个等级上)

[myWindow addSubview:anotherController.view];

anotherController将不会接受旋转事件,只有第一个view controller(primaryViewController)会接受旋转事件。


3.你添加了view controller的UIView属性到UIWindow作为subview,但是过早的release它。

UIWindow会retain视图,而不是view controller。你不能过早的release他。UIApplicationDelegate子类中定义他为retain属性。


4.在UITabBarController或者UINavigationController中的子view controller没有对同一方向的支持。

为了确保所有的子view controller旋转正确,你的每一个view controller,每一个tab或者额navigation都要完成shouldAutorotateToInterfaceOrientation,而且必须支持对于同一方向的旋转,也就是说对于同一方向的位置要返回为YES。


5.重写-(id)init:或者 -(id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle 方法的时候没有调用super。

对于对象的初始化,在你的view controller的init或者initWithNibName方法中必须要调用super。

欢迎光临小站 好岸园 http://www.hopean.com
原文地址:https://www.cnblogs.com/hopeanCom/p/3047033.html