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 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.
You might look at UIWindow. It inherits from UIView so it will have bounds and a frame, and iOS apps are all single window. -[UIApplication keyWindow] will return the window set up by the app delegate. Since all other views are anchored in the main window, I imagine this ought to give you correct bounds.
Actually that is very usefull.
When you ask a
UIScreen
for it'sBounds
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 theapplicationFrame
is usefull. There is no direct relationship between the 2 calls except that theapplicationFrame
is returned in theUIScreen bounds
coordinate system. (But the status bar is not part of your application, that explain the different result)I had a similar issue. What I was doing was pretty dumb, but I hope this helps someone.
In the subview I had:
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:
Swift 2.0 update: