How to get iPhone screen size programmatically? [duplicate]
Asked Answered
S

4

11

Possible Duplicate:
How to get screen size using code?

 NSLog(@"Top Left View : Width %f height%f",self.topLeftView.frame.size.width,self.topLeftView.frame.size.height);

I have dragged the "View" from object library and put it on xib file.

But what if I want to get the screen size to check whether its iphone 4,iPhone 3 or iphone 5 or any iOS Device.

So that I can arrange other views accordingly.

Shimmery answered 23/1, 2013 at 8:8 Comment(5)
use [[UIScreen mainScreen] bounds] or [[UIScreen mainScreen] applicationFrame]Collateral
Using the following code [UIScreen mainScreen].bounds.sizeOrvieto
very easy question. I wish for some other topics one could get 6 answers in that 30 sec while I typed ;)Yazzie
It is just that I have tried this portal first and you can get exact answer in reasonable time.Sometimes google gives more then you ask for ,so it becomes confusing.Shimmery
Why such duplicate questions are not flagged and voted for close? :/Andres
E
30

You can use:

CGsize screenSize      = [[[UIScreen mainScreen] bounds] size];
CGFloat widthOfScreen  = screenSize.width;
CGFloat heightOfScreen = screenSize.height;
Eaglewood answered 23/1, 2013 at 8:10 Comment(5)
You could have rather voted for closing this question instead of answering duplicate questions and entertaining Help Vampires who do not care to search before posting a question.Andres
@rohan-patel: Thanks for your comment. I'll do it.Eaglewood
Much appreciated. Please do not take that personally. This way site will remain clear and there will not be pile of duplicate questions. Hope you understand.Andres
@rohan-patel: Yes as a reviewer, I know. :)Eaglewood
If your app is designed for only landscape or portrait mode you will need to compare the width to the height to determine the correct size. We cannot get the orientation reliably anymore. For example, I have an app designed only for portrait mode but if it is opened in landscape mode on an iPad the width and height are mixed up.Compony
F
12

You can know it with the UIScreen properties

[[UIScreen mainScreen] bounds].size.height;
[[UIScreen mainScreen] bounds].size.width;

for iPhone 5 and iPod touch 5gen the height is 568

for other iPhones and iPods the height is 480

Edit: on iOS 8 the height and the width deppend on the orientation, so this sizes are for portrait, on landscape this sizes will be for the width instead. So, best choice is to read both and do the max

Footway answered 23/1, 2013 at 8:10 Comment(2)
why the iphone 5 and ipod touch 5gen the height is 568 not 1136?Kerin
Because it's the real size, not the pixel size, if you want the pixel size you have to multiply the [[UIScreen mainScreen] bounds].size.height * [UIScreen mainScreen].scale (you have to use something like this because scale isn't available in some cases CGFloat scale = ([mainScreen respondsToSelector:@selector(scale)] ? mainScreen.scale : 1.0f); )Footway
I
5

You can get the size of iPhone screen as

CGSize size = [UIScreen mainScreen].bounds.size;
Iconoclast answered 23/1, 2013 at 8:10 Comment(0)
Y
2
UIScreen *screen = UIScreen.mainScreen;

It has all you need including bounds and scale (may be different for retina and non-retina).

Yazzie answered 23/1, 2013 at 8:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.