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

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

问题描述

我已在我的代码中实现此方法,以了解何时会发生接口方向更改:

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方法吗?这感觉有点像黑客。

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:首次加载控制器时未调用持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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