iOS8:IScreen.mainScreen()。bounds不可靠,用于方向更改。建议?

时间:2015-09-07 12:34:19

标签: ios swift ios8 device-orientation

如果我使用IScreen.mainScreen()。bounds以便在方向改变后得到屏幕的大小,我有时会得到错误的" (="另一个")屏幕尺寸(与当前不显示的方向相关联)。我试图更新我从IScreen.mainScreen()得到的值。我直接在func rotate()中通过通知中心添加到我的控制器:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)
[...]
rotated() {
    myBounds = IScreen.mainScreen().bounds
}

然而,myBounds可以产生与偶尔当前不活动的方向相关的值(特别是在我最新一代iPad上非常快速地改变方向时)。我应该使用另一种方式来获得"屏幕尺寸?或者我应该从我的代码中的其他地方读取IScreen.mainScreen()。bounds中的值吗?

2 个答案:

答案 0 :(得分:0)

您必须使用从mainscreen()。bound绑定的维度,然后考虑方向。使用方法:

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

捕捉方向变化。当您使用willRotateToInterfaceOrientation时,即使您快速更改旋转,也应该能够捕获正在发生的方向更改。

答案 1 :(得分:0)

我似乎能够通过覆盖ViewController中的viewDidLayoutSubviews()来可靠地处理方向更改。我试过这个似乎工作正常。