Search code examples
iosobjective-cscreen-size

How can two variables to equal the screen size and width?


Hello I am trying to move images around depending on the screen size fore example iPhone 3.5" or 4". To do this I am pulling the screen size and then setting the center of the image to the screen width and height minus a certain amount. The only problem is the screen size is not changing like it should be. It is saying that the iPhone 3.5" and 4" have the same dimensions which they obviously do not. How do I fix this?

This is the current code I am using to pull the dimensions:

CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
Height = screenSize.width;
Width = screenSize.height;

Solution

  • Try this:

    #define kSizeScreenWidth         320
    #define kSizeScreenHeight        [[UIScreen mainScreen] bounds].size.height //480
    
    #define kDeviceVersionAvailable  3.2
    #define kDeviceVersion           [[[UIDevice currentDevice] systemVersion] floatValue]
    #define kDeviceScreenWidth       kDeviceVersion < kDeviceVersionAvailable ? kSizeScreenWidth : [[[UIScreen mainScreen] currentMode] size].width
    #define kDeviceScreenHeight      kDeviceVersion < kDeviceVersionAvailable ? kSizeScreenHeight : [[[UIScreen mainScreen] currentMode] size].height
    

    If your app device version >= 3.2, then just try this:

    #define kDeviceScreenWidth  [[[UIScreen mainScreen] currentMode] size].width
    #define kDeviceScreenHeight [[[UIScreen mainScreen] currentMode] size].height