"不正确"在iPhone中重新定位后的框架/窗口大小 [英] "Incorrect" frame / window size after re-orientation in iPhone

查看:150
本文介绍了"不正确"在iPhone中重新定位后的框架/窗口大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iPhone OS应用程序中,我希望(需要)观察设备方向的变化,以便重新排列屏幕的某些部分。我使用的方法是使用 CGRect frame = [UIScreen mainScreen] .applicationFrame 来获取屏幕大小,并从那里计算其他控件的大小和/或位置(I还试过 self.view.frame )。

In my iPhone OS application I want (need) to watch for changes in the device orientation in order to rearrange certain portions of the screen. The approach I used was to use CGRect frame = [UIScreen mainScreen].applicationFrame to get the screen size, and from there calculate the size and / or positioning of other controls (I also tried self.view.frame).

到目前为止,所有测试都是在人像模式下完成的,所以我可以专注于编程主要功能,稍后只对Landscape进行一些调整。这里输入问题:在 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 我在继续之前添加了一些日志以检查大小,但显然宽度和高度的值是错(我说错,因为乍一看价值对我来说没有意义)。

All testing was done so far in Portrait mode, so I could focus on programming the main features and later on just do some adjustments for Landscape. And here enters the problem: In -(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation I added some logging to check the sizes before proceeding, but apparently the values for Width and Height are "wrong" (I say "wrong" because at a first glance the values does not make sense to me).

以下是一些记录的输出:

Here's the output of some logging:


  • 轮换:横向[w = 300.000000,h = 480.000000]

  • 轮换:人像[w = 320.000000,h = 460.000000]

Landscape中w和h的值似乎与我相反 - 我期待w = 480和h = 300。

The values for "w" and "h" in Landscape seem inverted to me - I was expecting that w=480 and h=300.

我做错了什么?我用来调试的代码如下。

What am I doing wrong? The code I used to debug is below.

-(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]);
}


推荐答案

设备的方向已更改,而不是屏幕的物理特性。你基本上把它放在一边,但实际上它是320像素宽(其中20个在状态栏显示后暂时无法使用)和480像素高。如果您的视图是自动旋转的,那么宽度/高度已经为您翻译,但是当您询问屏幕的实际尺寸时,您将获得屏幕的实际尺寸。

The orientation of your device changed, not the physical characteristics of the screen. You basically tipped it on its side, but in reality it is 320 pixels wide (20 of which are not available to you at the moment since the status bar is showing) and 480 pixels tall. If your view is auto-rotating, then the width/height have been translated for you, but when you ask for the actual dimensions of the screen, you get back the actual dimensions of the screen.

这也是为什么在处理翻译的视图时,重要的是根据视图的中心和视图的边界进行计算,而不要在视图的框架上进行计算。

This is also why, when working with translated views, it is important to do calculations based on the view's center and the view's bounds and never on the view's frame.

这篇关于"不正确"在iPhone中重新定位后的框架/窗口大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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