Why won't my UIViewController rotate with the device?

本文详细解释了UIViewController在iOS应用中无法随设备旋转的原因及解决方法,包括但不限于未实现shouldAutorotateToInterfaceOrientation方法、额外视图控制器嵌套、过早释放窗口视图控制器等问题,并提供了针对性的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Q:  Why won't my UIViewController rotate with the device?


A: Why won't my UIViewController rotate with the device?

There can be several possible reasons your view controller does not rotate.

The UIViewController class provides the fundamental view-management model for iPhone applications. It provides automatic support for rotating the views of the view controller in response to changes to the orientation of the device. If the autoresizing properties of your view and subviews are properly configured, this behavior is essentially free. Your view controller may not rotate in certain situations if:

  • The view controller does not implement this delegate method:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;

    Even if shouldAutorotateToInterfaceOrientation is implemented, you should make sure it returns YES for all the orientations you wish to support. To support all orientations, simply always return YES.

  • The view controller's UIView property is embedded inside UIWindow but alongside an additional view controller.

    You may find a situation where shouldAutorotateToInterfaceOrientation is called once at startup for a given view controller but is never called again when the device is rotated. Because view controllers are tightly bound to the views they manage, they are also part of the responder chain used to handle events. View controllers are themselves descendants of the UIResponder class and are inserted into the responder chain between the managed view and its superview. So it is common practice to have one primary view controller in your application as part of the responder chain. You would typically add one primary view controller such as aUINavigationControllerUITabBarController or a generic UIViewController to your UIWindow. For example, this is done by calling:

    [myWindow addSubview:primaryViewController.view];

    If you add an additional view controller's UIView property to UIWindow (at the same level as your primary view controller) via the following:

    [myWindow addSubview:anotherController.view];

    this additional view controller will not receive rotation events and will never rotate. Only the first view controller added to UIWindow will rotate.

    Note: You may add one view controller's UIView property as a subview to another view controller. In doing so, both views will rotate but the parent view controller remains in charge of determining the supported orientations via its shouldAutorotateToInterfaceOrientation method.

    Note: Keep in mind that UINavigationController and UITabBarController have the capability to manage a "stack" or "list" of view controllers on their own.

  • You have added your view controller's UIView property to UIWindow as a subview, but prematurely released it soon after.

    UIWindow will retain that view, but not the view controller itself. You must not release it prematurely. Declare it as a "retainable" property in yourUIApplicationDelegate subclass.

    Note: UINavigationController and UITabBarController retain your view controllers, so you can release them once they have been added.

  • All child view controllers in your UITabBarController or UINavigationController do not agree on a common orientation set.

    To make sure that all your child view controllers rotate correctly, you must implement shouldAutorotateToInterfaceOrientation for each view controller representing each tab or navigation level. Each must agree on the same orientation for that rotate to occur. That is, they all should return YES for the same orientation positions.

  • Overriding the -(id)init: or -(id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle method without calling super. For the object to be initialized properly, you must call super on any init or initWithNibName method you are overriding for your view controllers.


资源下载链接为: https://pan.quark.cn/s/abbae039bf2a 在计算机科学领域,编译原理是研究如何将编程语言转化为机器可执行代码的理论基础。其中,三地址代码(Three-Address Code,TAC)作为一种中间表示形式,在编译器设计中经常被使用,尤其是在生成目标代码的阶段。本文将深入探讨三地址代码的概念、生成器的工作原理及其在编译过程中的作用。 三地址代码是一种简单的低级抽象语法树(AST)表示,每条指令涉及三个操作数,通常包括两个源操作数和一个目的操作数。这种格式简化了代码优化和目标代码生成的复杂性。例如,一个简单的算术表达式“x = y + z”在三地址代码中可能表示为: 在这个例子中,“t1”是一个临时变量,存储了“y + z”的结果,然后这个结果被赋值给“x”。 生成三地址代码的过程通常发生在编译器的中间阶段,即语法分析之后,语义分析之前。这个阶段称为“代码生成”或“中间代码生成”。编译器通过词法分析器处理源代码,将其转化为标记流;接着,语法分析器根据上下文无关文法将标记流解析成抽象语法树。三地址代码生成器就是在这个阶段介入,它遍历AST,为每个节点生成对应的三地址指令。 在Turbo C3.0这样的编译器环境下,开发者可以实现自己的三地址代码生成器。虽然Turbo C3.0是一款较老的编译器,但其C语言编译器设计原理依然适用于现代编译器开发。开发过程中,我们需要考虑如下关键点: 符号表管理:符号表记录了程序中所有标识符的类型、作用域和关联地址,对于生成三地址代码至关重要,因为它提供了关于操作数的类型信息。 数据类型转换:编译器必须处理不同数据类型的运算,确保它们在三地址代码中正确表示。例如,整型与浮点型之间的转换需要特别处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值