如何获得实际的 [UIScreen mainScreen] 帧大小? [英] How to get the actual [UIScreen mainScreen] frame size?

查看:19
本文介绍了如何获得实际的 [UIScreen mainScreen] 帧大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点生气.我有一个在主窗口中可见状态栏的应用程序.因为我想动态设置我的视图和它们的框架大小(例如,状态栏在打电话时会占用 40 像素).

I'm a bit annoyed. I have an app with the statusbar visible in the main window. Since I would like to setup my views and their frame sizes dynamically (perhaps the status bar takes up 40 pixels during a phone call, for example).

我可以做两个之一:

[[UIScreen mainScreen] bounds];
[[UIScreen mainScreen] applicationFrame];

真正令人讨厌的是这两个输出两组不同的值,每组都同样无用.

The really annoying this is these two output two different sets of values, each equally as useless.

bounds 将输出:{{0, 0}, {320, 480}}
applicationFrame 将输出 {{0, 20}, {320, 460}}

bounds will output: {{0, 0}, {320, 480}} while
applicationFrame will output {{0, 20}, {320, 460}}

如您所见,bounds 给出了正确的 y 原点(0 从状态栏正下方开始),但随后给出的高度为 480,这是不正确的.它应该是 460,因为状态栏是可见的.然后我们有 applicationFrame 它从状态栏下方 20 像素开始(所以有一个上限),但随后给出了正确的高度.但无论如何,当它被向下推 20 个像素时,这并不是很有用.

As you can see, bounds gives the correct y origin (0 starts from right below the statusbar) but then gives a height of 480, which is incorrect. It should be 460, since the statusbar is visible. Then we have applicationFrame which starts 20 pixels below the statusbar (so there's a cap), but then gives the correct height. But that's not very useful when it's then pushed down 20 pixels anyway.

有什么帮助吗?

推荐答案

其实这很有用.
当你向 UIScreen 询问它的 Bounds 时,你会得到屏幕的边界,也就是整个设备屏幕.(状态栏是屏幕的一部分)
但是,如果您要求 UIScreen 告诉您应用程序的根视图的位置和大小,请求 applicationFrame 会很有用.除了在 UIScreen bounds 坐标系中返回 applicationFrame 之外,这 2 个调用之间没有直接关系.(但状态栏不是您的应用程序的一部分,这说明了不同的结果)

Actually that is very usefull.
When you ask a UIScreen for it's Bounds you get the bounds of the screen, which is the whole device screen. (the status bar is part of the screen)
But if you ask a UIScreen to tell you where and how big can be the root view of your application asking for the applicationFrame is usefull. There is no direct relationship between the 2 calls except that the applicationFrame is returned in the UIScreen bounds coordinate system. (But the status bar is not part of your application, that explain the different result)

applicationFrame
用于应用程序窗口的框架矩形.(只读)
@property(nonatomic, readonly) CGRect applicationFrame
讨论
此属性包含屏幕边界减去状态栏占用的区域(如果状态栏可见).使用此属性是检索应用程序初始窗口大小的推荐方法.矩形以点为单位指定.

applicationFrame
The frame rectangle to use for your application’s window. (read-only)
@property(nonatomic, readonly) CGRect applicationFrame
Discussion
This property contains the screen bounds minus the area occupied by the status bar, if it is visible. Using this property is the recommended way to retrieve your application’s initial window size. The rectangle is specified in points.

这篇关于如何获得实际的 [UIScreen mainScreen] 帧大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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