How to get the actual frame size [UIScreen mainScreen]?

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

I can do one of two things:

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

In fact, it annoys these two outputs with two different sets of values, each of which is equally useless.

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

As you can see, bounds gives the correct y origin (0 starts to the right of the status bar), but then gives a height of 480, which is incorrect. It should be 460 because the status bar is visible. Then we have an applicationFrame that starts 20 pixels below the status bar (so there is a cap), but then gives the correct height. But this is not very useful when in any case it pushes 20 pixels.

Any help?

+45
uiview frame uiapplication
Mar 01 '11 at 5:21
source share
5 answers

This is actually very helpful.
When you ask UIScreen for Bounds for it, you get the borders of the screen, which is the screen of the entire device. (status bar is part of the screen)
But if you ask UIScreen tell you where and how big the root look of your application might be, requesting the applicationFrame is useful. There is no direct connection between the two calls, except that the applicationFrame returned in the UIScreen bounds coordinate system. (But the status bar is not part of your application, which explains a different result)

applicationFrame
The frame to use for your application window. (Only for reading)
@property (nonatomic, readonly) CGRect applicationFrame
Discussion
This property contains the borders of the screen minus the area occupied by the status bar, if visible. Using this property is the recommended way to get the initial size of your application. The rectangle is indicated in points.

+49
Aug 13 '12 at 1:50
source share

In fact, 0 does not start at the bottom of the status bar. It starts from the top. Add a UILabel to (0,0) and you will not see it unless you have a status bar. Thus, the borders give you the total screen area, and the applicationFrame gives you the area that your application should work with. I bet if you hide the status bar, the application frame will correspond to the borders.

+8
Jul 08 '11 at 15:52
source share

I had a similar problem. What I did was pretty dumb, but I hope this helps someone.

In the subtitle, 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 created my subview with a frame with a source other than (0,0), but then using the same frame to create subviews for my subview, these subviews were also pushed down. The result was an annoying black banner.

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; } 
+1
Apr 22 '12 at 1:23
source share

You can look at UIWindow. It inherits from UIView, so it will have borders and frame, while iOS apps will have one window. - [UIApplication keyWindow] will return the window configured by the application delegate. Since all other views are anchored in the main window, I believe that this should give you the correct boundaries.

0
Mar 01 2018-11-11T00:
source share

Swift 2.0 Update:

 func presentAboveAll() { //Get the view from the nib. Assuming the view is is in AboveAllView.xib let nibContents = NSBundle.mainBundle().loadNibNamed("AboveAllView", owner: nil, options: nil) //Get hold of your view let views = nibContents.filter { if let _ = $0 as? UIView{ return true } return false } guard views.count > 0 else { return } //Set up frame and add to the app key window let view = views.first as! UIView view.frame = UIScreen.mainScreen().bounds UIApplication.sharedApplication().keyWindow?.addSubview(view) } 
-one
Dec 24 '15 at 9:49
source share



All Articles