iOS:ViewDidAppear上的方向检查旋转 [英] iOS: Orientation check rotation on ViewDidAppear

查看:132
本文介绍了iOS:ViewDidAppear上的方向检查旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

后台:我想确保我的viewControllers在出现时正确旋转。我的viewControllers具有很好的代码,可以在可见时管理旋转和方向。

Background: I want to make sure my viewControllers rotate properly when it appears. My viewControllers have excellent codes managing the rotation and orientation when it is visible.

问题:在NavigationController中给出两个viewControllers, viewC1 viewC2 。我做了以下事情:

Problem: Given two viewControllers in a NavigationController, viewC1 and viewC2. I did the following:

1。)将rootViewController设置为 viewC1

1.) Set rootViewController to viewC1

2。)将 viewC2 推入NavigationController

2.) Push viewC2 into the NavigationController

3。)旋转

4。)Pop viewC2

5。) viewC1 仍然停留在旧方向外观中(如在中的转换代码中> willAnimateRotationToInterfaceOrientation 未调用)具有新方向。

5.) viewC1 is still stucked in the old orientation look (as in the transformation code in willAnimateRotationToInterfaceOrientation was not called) with the new orientation.

我该怎么做才能确保 viewC1 调用 willAnimateRotationToInterfaceOrientation 进行重建本身在新的轮换中正确看待?

What can I do to ensure viewC1 call willAnimateRotationToInterfaceOrientation to reconstruct itself to look correctly in the new rotation?

其他信息:
这是所有代码(没有storyboard / xib)。我在所有视图上都有 shouldAutorotateToInterfaceOrientation 返回 YES 。我使用 willAnimateRotationToInterfaceOrientation 来管理我的所有轮换。

Additional info: This is all code (no storyboard/xib). I have shouldAutorotateToInterfaceOrientation return YES on all the views. I use willAnimateRotationToInterfaceOrientation to manage all my rotation.

哦,请不要乱砍。例如,从旋转中复制代码然后手动检查旋转并在 viewDidAppear 中管理它。

Oh, and please no hacks. For example, copy the code from rotation then check the rotation mannually and manage it in viewDidAppear.

推荐答案

想想方法的名称,以及你想要实现的目标。

Think about the name of the method, and what you're trying to achieve.

willAnimateRotationToInterfaceOrientation 表示视图控制器控制的视图即将为特定方向设置动画。如果您的视图位于导航堆栈的中间,则它不会显示在屏幕上。为不在屏幕上的东西制作动画是昂贵的,最终毫无价值。所以,这种特殊方法是不可能的,但剩下的问题是在UIKit中没有其他更合适的方法。原因是当它在屏幕外是无价值的时候旋转某些东西(即使没有动画)。当视图出现时,开发人员有责任处理方向的变化(按需转换)。

willAnimateRotationToInterfaceOrientation indicates that the view controlled by the view controller is about to animate to a particular orientation. If your view is in the middle of a navigation stack, then it is not being displayed on screen. To animate something that isn't on screen is costly and ultimately worthless. So, that particular method is out of the question, but the problem that remains is there isn't anything else more appropriate in UIKit. The reason is to rotate something (even if not animated) when it's offscreen is worthless cost. It's the responsibility of the developer to handle a change in orientation when the view appears ("transformation on demand" as you will).

你说你不想要黑客攻击,但你所描述的黑客攻击方法可能是最合适的方法。有一个名为
的通用方法 - (void)updateLayoutForOrientation:(UIInterfaceOrientation)方向动画:(BOOL)动画{...}
isn不错的主意。这可以是整个视图控制器的方向更改转换的处理程序。

You say you don't want hacks, but the method you've described as a hack is probably the most appropriate thing to do. Having a generic method named something like -(void) updateLayoutForOrientation:(UIInterfaceOrientation)orientation animated:(BOOL)animated { ... } isn't a bad idea. This can be the handler for orientation change transformations for the whole view controller.

您需要检查/处理方向问题的位置是

The places you need to possibly check/handle orientation issues are

- (void)viewWillAppear:(BOOL)animated

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration

并且在这两者中,调用 updateLayoutForOrientation:animated:为你做的工作。

and in both of these, call updateLayoutForOrientation:animated: to do the work for you.

这篇关于iOS:ViewDidAppear上的方向检查旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆