Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid hardcoding iPhone screen size with programmatic view creation

Tags:

iphone

uiview

I was looking up how to create a view programmatically and found the following example code:

self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

This works great, except I don't like that it hardcodes the size of the screen. Is there a way to look up the size of the screen? An important point someone brought up is that if the app is running during a phone call then the screen will be slightly smaller because of the green "return to call" bar.

like image 661
miorel Avatar asked Mar 27 '10 19:03

miorel


2 Answers

Is there a way to look up the size of the screen?

Yup:

CGRect screenRect = [[UIScreen mainScreen] applicationFrame];

applicationFrame

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.

like image 160
Shaggy Frog Avatar answered Nov 15 '22 13:11

Shaggy Frog


[[UIScreen mainScreen] bounds] or [[UIScreen mainScreen] applicationFrame]

should be all you need to get the full screen size.

I've only used the applicationFrame version myself.

(I also suggested [UIHardware fullScreenApplicationContentRect] but that was from some very old code - don't use it)

like image 20
Andiih Avatar answered Nov 15 '22 13:11

Andiih