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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
bhavya kothari
  • 7,484
  • 4
  • 27
  • 53

4 Answers4

30

You can use:

CGsize screenSize      = [[[UIScreen mainScreen] bounds] size];
CGFloat widthOfScreen  = screenSize.width;
CGFloat heightOfScreen = screenSize.height;
Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • 4
    *You could have rather voted for closing this question instead of answering duplicate questions and entertaining [**Help Vampires**](http://slash7.com/2006/12/22/vampires/) who do not care to search before posting a question.* – rohan-patel Jan 23 '13 at 09:45
  • 1
    @rohan-patel: Thanks for your comment. I'll do it. – Midhun MP Jan 23 '13 at 09:47
  • 1
    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. – rohan-patel Jan 23 '13 at 09:50
  • @rohan-patel: Yes as a reviewer, I know. :) – Midhun MP Jan 23 '13 at 09:52
  • 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. – Bobby Feb 28 '16 at 22:38
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

itsji10dra
  • 4,603
  • 3
  • 39
  • 59
jcesarmobile
  • 51,328
  • 11
  • 132
  • 176
  • why the iphone 5 and ipod touch 5gen the height is 568 not 1136? – chancyWu Jan 23 '13 at 09:09
  • 1
    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); ) – jcesarmobile Jan 23 '13 at 09:23
  • thanks, i think i know the reason. – chancyWu Jan 23 '13 at 09:25
5

You can get the size of iPhone screen as

CGSize size = [UIScreen mainScreen].bounds.size;
ask4asif
  • 676
  • 4
  • 10
2
UIScreen *screen = UIScreen.mainScreen;

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

Cynichniy Bandera
  • 5,991
  • 2
  • 29
  • 33