Xcode - How do I Detecting screen size of iPhone

18,213

Solution 1

add below line in your prefix.pch file...this is the simplest way to check screen size, no need to make extra lines of code...

#define   IsIphone5     ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

now whereever you want to check screen size , just make condition like below, and you can do whatever you want....

 if(IsIphone5)
{
    //your stuff
}
else
{  
  //your stuff
}

Happy Coding!!!!

Solution 2

put this condition

if ((int)[[UIScreen mainScreen] bounds].size.height == 568)
{
    // This is iPhone 5 screen
} else {
    // This is iPhone 4/4s screen
}

Solution 3

remove extra ")" at the end from this line

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone))

Solution 4

Try this remove last Bracket

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
      CGSize result = [[UIScreen mainScreen] bounds].size;
      if(result.height == 480)
      {
            //Load 3.5 inch xib
      }
      else if(result.height == 568)
       {
             //Load 4 inch xib
       }
}
Share:
18,213

Related videos on Youtube

Brad Conidaris
Author by

Brad Conidaris

Updated on June 07, 2022

Comments

  • Brad Conidaris
    Brad Conidaris almost 2 years

    I have two xibs, one for the iPhone 4 and one for the iPhone 5; 3.5 inch and 4 inch. I simply want to put some code that tells the app which to load. I have this code which is supposed to do the job:

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone))
    {
        CGSize result = [[UIScreen mainScreen] bounds].size;
        if(result.height == 480)
        {
        //Load 3.5 inch xib
        }
        if(result.height == 568)
        {
            //Load 4 inch xib
        }
    

    I put it in the ViewController.M(is that where I put it?) and the build fails saying there is a parse issue expected identifier of "(" Can anyone help me with this simple fix? Thanks!

    • Buntylm
      Buntylm over 10 years
      if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) use this line
    • Martin R
      Martin R over 10 years
      This question appears to be off-topic because it is about a typo.
  • rmaddy
    rmaddy over 10 years
    This doesn't answer the question which is about the fact that the given code doesn't compile.
  • rmaddy
    rmaddy over 10 years
    This doesn't answer the question which is about the fact that the given code doesn't compile.
  • NiravPatel
    NiravPatel over 10 years
    this may not the answer of the question, but this answer is the simplest and smartest way to detect screen size my friend.Why should write 3 or 4 lines of code for just detecting the screen size..
  • rmaddy
    rmaddy over 10 years
    -1 for adding a duplicate answer.
  • Brad Conidaris
    Brad Conidaris over 10 years
    Which file do I put the code in? Appdelate.m?
  • D-eptdeveloper
    D-eptdeveloper over 10 years
    @BradConidaris : When you're loading your nib for 3.5 or 4 inch screen you can check this condition and load your nib accordingly
  • Brad Conidaris
    Brad Conidaris over 10 years
    Can you elaborate on that? Confused about nibs
  • D-eptdeveloper
    D-eptdeveloper over 10 years
    @BradConidaris : When you're pushing your viewController with navigation at there you can check for this condition and load the xib file of your requirement.
  • Dhaval Bhadania
    Dhaval Bhadania over 10 years
    @NiravPatel can you explain in brife why it is smart answer?
  • NiravPatel
    NiravPatel over 10 years
    @DhavalBhadania :Ghare aavje explain karis....
  • Dhaval Bhadania
    Dhaval Bhadania over 10 years
    @NiravPatel but it not working in my code ....can u explain how to used it ...really yar nahi chal tu ?
  • S.H.
    S.H. almost 10 years
    +1 Perfect... worked by just copy pasting that Macro code. Thanks
  • Jeff
    Jeff about 8 years
    @NiravPatel Best most effective answer on this topic EVER!
  • Gajendra K Chauhan
    Gajendra K Chauhan about 7 years
    yes +1 for good answer

Related