在我的 iPhone OS 应用程序中,我希望(需要)观察设备方向的变化,以便重新排列屏幕的某些部分。我使用的方法是用来CGRect frame = [UIScreen mainScreen].applicationFrame
获取屏幕大小,并从那里计算其他控件的大小和/或位置(我也尝试过self.view.frame
)。
到目前为止,所有测试都是在纵向模式下完成的,因此我可以专注于对主要功能进行编程,然后再对横向进行一些调整。这里出现了问题:在-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
继续之前,我添加了一些日志来检查尺寸,但显然 Width 和 Height 的值是“错误的”(我说“错误”,因为乍一看这些值对我来说没有意义) .
这是一些日志记录的输出:
- 旋转:横向 [w=300.000000, h=480.000000]
- 旋转:纵向 [w=320.000000,h=460.000000]
Landscape中“w”和“h”的值对我来说似乎是倒置的——我期待w=480和h=300。
我究竟做错了什么?我用来调试的代码如下。
-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
CGRect frame = [UIScreen mainScreen].applicationFrame;
CGSize size = frame.size;
NSLog(@"%@", [NSString stringWithFormat:@"Rotation: %s [w=%f, h=%f]",
UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? "Portrait" : "Landscape",
size.width, size.height]);
}