ios - self.view.frame.size 在同一方向改变值

标签 ios objective-c view orientation

一些对我来说很奇怪的问题:

- (void)horizontalPickerView:(HorizontalPickerView *)picker didSelectElementAtIndex:(NSInteger)index {
    if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
        self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        sdashSize = CGRectMake(0.0f, 40.0f, self.view.frame.size.width, self.view.frame.size.height);
    } else {
        sdashSize = CGRectMake(0.0f, 40.0f, self.view.frame.size.height, self.view.frame.size.width - 40.0f);
    }
}

如您所见,我有一个选择器 View 。这个方法每次运行,我在这个选择器 View 中选择了一些东西。所以现在如果我将我的 iPhone 旋转到横向并使用此代码转到下一个 View ,它运行一次 -> 转到 if 状态并给我尺寸:320 x 480。所以现在选择相同方向的一些元素并运行相同代码(如果再次声明)给我 480 x 320。我不明白相同方向的相同代码怎么会给我不同的值。

最佳答案

不要使用 frame 属性,使用 bounds 属性。本质上:

  • View 的框架 (CGRect) 是其矩形在父 View 坐标系中的位置。默认情况下,它从左上角开始。

  • View 的边界 (CGRect) 在其自己的坐标系中表示 View 矩形。

This answer进一步深入解释正在发生的事情。

关于ios - self.view.frame.size 在同一方向改变值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24866949/

相关文章:

iphone - 如何在不剪裁的情况下缩小 UIScrollView 中的图像?

iOS Domain=kCLErrorDomain Code=1 在请求位置权限之前

jsf-2 - 何时使用 f :view and f:subview

sql-server - SQL View : Join tables without causing the data to duplicate on every row?

ios - 类型 "ViewController"的 Swift 值没有名为 "Self"的成员错误

iphone - 如何判断方法错误(Objective C/iPhone/xCode)?

iphone - Objective-C 中的对象和 View 设置和初始化

android - 如何判断 Android Activity 何时完成加载?

ios - 即使在 DispatchQueue.main.async Swift 3 中设置后,数组仍然为空

iphone - 初学者 : Multiview app doesn't switch views correctly