objective-c - 为什么 self.view 的高度和宽度与 subview 的高度和宽度不同?

标签 objective-c ios xcode view dimensions

无论方向如何,主屏幕和 self.view 始终保持相同的宽度和高度尺寸(即使 self.view 正确调整大小以适应当前设备方向)这一事实背后的逻辑是什么,而self.view 的 subview 在设备方向改变时返回修改后的宽度和高度尺寸?

NSLog(@"screen bounds w: %f, h: %f", [[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height );

NSLog(@"self.view frame w: %f, h: %f", self.view.frame.size.width, self.view.frame.size.height );

NSLog(@"myView frame w: %f, h: %f", _myView.frame.size.width, _myView.frame.size.height );

日志输出纵向

2012-11-05 16:15:11.152 test[2807:11303] screen bounds w: 320.000000, h: 480.000000
2012-11-05 16:15:11.153 test[2807:11303] self.view frame w: 320.000000, h: 460.000000
2012-11-05 16:15:11.153 test[2807:11303] myView frame w: 320.000000, h: 460.000000

日志输出景观

2012-11-05 16:14:38.800 test[2807:11303] screen bounds w: 320.000000, h: 480.000000
2012-11-05 16:14:38.801 test[2807:11303] self.view frame w: 300.000000, h: 480.000000
2012-11-05 16:14:38.801 test[2807:11303] myView frame w: 480.000000, h: 300.000000

最佳答案

实际上它不仅仅是自动调整大小的蒙版。因此,当您更改设备的方向时,屏幕的物理特性保持不变。如果您将 View 设置为自动旋转,则会为您翻译宽度和高度。因此,当要求带有翻译 View 的屏幕尺寸时,它的影响。您可以根据 self.view.centerself.view.bounds 而不是 self.view.frame 进行计算。

关于objective-c - 为什么 self.view 的高度和宽度与 subview 的高度和宽度不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13228952/

相关文章:

iphone - 将 iPad/iPhone 应用程序与 Rails 应用程序集成

ios - 对 textFieldDidEndEditing 的操作

objective-c - 用于滑动文本的 NSTextField 子类 - Objective-C/Cocoa

ios - 查找 Xcode 项目的 ${PROJECT_DIR}

ios - 解析推送通知,收到通知时如何修改当前 View Controller 中的变量?

iphone - 删除 View 以在 ScrollView 的前一个点显示前一个 View

iphone - substringWithRange收到一个奇怪的错误

ios - RealmJS : clang: error: linker command failed with exit code 1 (use -v to see invocation)

ios - watchOS2 CMDeviceMotion 不起作用

ios - 如何从iOS应用中清除保存的密码或表单数据