iOS 5:第一次加载控制器时未调用 willRotateToInterfaceOrientation:duration [英] iOS 5: willRotateToInterfaceOrientation:duration not called when first loading controller

查看:16
本文介绍了iOS 5:第一次加载控制器时未调用 willRotateToInterfaceOrientation:duration的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的代码中实现了这个方法来了解界面方向何时会发生变化:

I've implemented this method in my code to know when an interface orientation change will occur:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

我依靠这个被调用来设置我的视图.在 iOS 4 或 5 中,当方向改变时,它会被正确调用.

I rely on this to be called to setup my views. In either iOS 4 or 5 it gets called properly when the orientation changes.

在 iOS 4 中,它也会在控制器首次加载时被调用(无论方向是否改变,它都会在开始时以正确的方向调用一次).

In iOS 4 it also gets called when the controller first loads (regardless of if the orientation changes or not, it gets called once at the beginning with the correct orientation).

问题是我注意到在 iOS 5 中这种情况不再发生.该方法在方向改变时被调用,但在控制器最初加载时不会被调用.这对我来说是个问题,因为我依靠它来根据方向设置初始视图位置.

The problem is I noticed in iOS 5 this does not happen anymore. The method gets called when the orientation changes but not when the controller initially loads. This is a problem for me because I rely on this to setup the initial view placement based on the orientation.

任何想法为什么这种行为会改变?处理这个问题的最佳方法是什么?如果在 iOS 5 上,我应该检查 viewDidLoad 中的方向,然后手动调用 willRotate 和 didRotate 方法吗?这感觉有点像 hack.

Any ideas why this behaviour changed? What's the best way to handle this? Should I check what the orientation is in viewDidLoad if on iOS 5 and then manually call the willRotate and didRotate methods? This feels a bit like a hack.

感谢您提供的任何意见.

Thanks for any input you can provide.

推荐答案

由于时间紧迫,我不得不解决这个奇怪的行为并手动调用 viewDidLoad 中的定位方法.这有点小技巧,但到目前为止它运行良好.

Since I'm pressed for time I've had to work around this odd behaviour and manually call the orientation methods in viewDidLoad. This is a bit of a hack but it's working fine so far.

viewDidLoad 我添加了这个:

if ( SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0") ) {
    UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    [self willRotateToInterfaceOrientation:interfaceOrientation duration:0.2f];
}

然后我将它添加到一个通用头文件中:

I've then added this in a common header file:

// iOS Version Checking
#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

我从这个问题的答案中得到了iOS版本检查定义.

I got the iOS version checking defines from an answer to this question on SO.

这篇关于iOS 5:第一次加载控制器时未调用 willRotateToInterfaceOrientation:duration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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