如何获得实际的[UIScreen mainScreen]框架尺寸?[英] How to get the actual [UIScreen mainScreen] frame size?

本文是小编为大家收集整理的关于如何获得实际的[UIScreen mainScreen]框架尺寸?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有点生气.我有一个在主窗口中可见的应用程序栏.由于我想动态设置我的视图及其框架大小(例如,状态栏在电话中占用40个像素).

我可以做两者之一:

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

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

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

如您所见,bounds给出了正确的y origin(0从状态栏下方下方开始),但然后给出480的高度,这是不正确的.由于现状栏是可见的,应该是460.然后,我们的applicationFrame在状态栏以下启动20个像素(因此有一个帽子),但然后给出正确的高度.但是,当它被推下20像素时,这不是很有用.

有帮助吗?

推荐答案

实际上非常有用.
当您向A UIScreen提出Bounds时,您会获得屏幕的界限,这是整个设备屏幕. (状态栏是屏幕的一部分)
但是,如果您要求A UIScreen告诉您应用程序的根视图在哪里,要求applicationFrame有用. 两个调用之间没有直接关系,除了applicationFrame在UIScreen bounds坐标系中返回. (但是状态栏不是您申请的一部分,可以解释不同的结果)

applicationframe 用于应用程序窗口的框架矩形. (只读)
@property(非原子,readonly)cgrect applicationframe
讨论
该属性包含屏幕界限,如果可见,则将状态栏所占据的区域减去.使用此属性是检索应用程序初始窗口大小的推荐方法.矩形在点中指定.

其他推荐答案

实际上0不会从状态栏的底部开始.它从顶部开始.在(0,0)中添加一个Uilabel,除非隐藏状态栏,否则您不会看到它.因此,界限为您提供了整个屏幕区域,并且应用程序框为您的应用程序必须使用的区域.我敢打赌,如果隐藏状态栏,应用程序框架将与边界匹配.

其他推荐答案

我也有类似的问题.我正在做的事很愚蠢,但我希望这对某人有帮助.

在我有:

的子视图中
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIView * otherSubview = [[UIView alloc] initWithFrame:frame];
        [self addSubview:otherSubview];
    }
    return self;
}

因为我正在用(0,0)的原点的框架创建我的子视图,但随后使用相同的框架为我的子视图生成子视图,所以这些子视图也被转移了.结果是您提到的令人讨厌的黑色横幅.

我通过执行以下操作来解决此问题:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        CGRect rect = frame;
        rect.origin.x = 0;
        rect.origin.y = 0;
        UIView * otherSubview = [[UIView alloc] initWithFrame:rect];
        [self addSubview:otherSubview];
    }
    return self;
}

本文地址:https://www.itbaoku.cn/post/2091127.html

问题描述

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).

I can do one of the two:

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

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

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

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.

Any help?

推荐答案

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
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.

其他推荐答案

Actually 0 does not start at the bottom of the status bar. It starts at the top of it. Add a UILabel at (0,0) and you won't see it unless you have the status bar hidden. So bounds gives you the total screen area, and applicationFrame gives you the area your application has to work with. I bet if you hide the status bar the application frame will match the bounds.

其他推荐答案

I had a similar issue. What I was doing was pretty dumb, but I hope this helps someone.

In the subview I had:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIView * otherSubview = [[UIView alloc] initWithFrame:frame];
        [self addSubview:otherSubview];
    }
    return self;
}

Because I was creating my subview with a frame with an origin not at (0,0), but then using that same frame to generate subviews for my subview, those subviews also got shifted down. The result was that annoying black banner you mentioned.

I fixed this by doing the following:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        CGRect rect = frame;
        rect.origin.x = 0;
        rect.origin.y = 0;
        UIView * otherSubview = [[UIView alloc] initWithFrame:rect];
        [self addSubview:otherSubview];
    }
    return self;
}