1

I'm creating universal app that runs oniphone and ipad. I'm using #define to create CGRect. And I want to use two different #define - one for iPhone and one for iPad. How can I declare them so that correct one will be picked by universal app..........

I think I've to update little more description to avoid confusion. I've a WPConstants.h file where I'm declaring all the #define as below

#define PUZZLE_TOPVIEW_RECT CGRectMake(0, 0, 480, 100)
#define PUZZLE_MIDDLEVIEW_RECT CGRectMake(0, 100, 480, 100)
#define PUZZLE_BOTTOMVIEW_RECT CGRectMake(0, 200, 480, 100)

The above ones are for iphone. Similarly for iPad I want to have different #define How can I proceed further?

Satyam
  • 15,493
  • 31
  • 131
  • 244

2 Answers2

2

As recommended by Apple, use

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { ... }
else { ... }

to write platform-specific code. With the ternary ?: operator, you could also incorporate this into a #define:

#define MyRect (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? CGRectMake(0,0,1024,768) : CGRectMake(0,0,480,320))

In case you wanted to use conditional compilation to determine which of two #define statements should be included in your code, you can't: a universal app does not contain two separate binaries for iPhone and iPad. It's just one binary so all platform-related decisions have to be made at runtime.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • Thanks for the info. You mentioned CGRectMake for only one rect. Please see my updated description. – Satyam Dec 27 '10 at 17:51
  • Well, you would obviously repeat the process three times. BTW, I wouldn't work with `#define`s. Why not just declare a variable and define it according to the platform you're running on. – Ole Begemann Dec 27 '10 at 19:08
  • Thanks again. Can you tell me how to declare variables account to the platform I'm running on? – Satyam Dec 28 '10 at 03:10
  • Just declare a variable: `CGRect puzzleTopViewRect;`, then set its value depending on the result of `UI_USER_INTERFACE_IDIOM()`. – Ole Begemann Dec 31 '10 at 14:51
1

i used this function to detect iPad, and then write conditions for all different parts of my application.

#ifdef UI_USER_INTERFACE_IDIOM
    #define isPad() (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#else
    #define isPad() NO
#endif

Also you can load different xib files for iPhone/iPad.

Evgen Bodunov
  • 5,438
  • 2
  • 29
  • 43
  • #ifdef UI_USER_INTERFACE_IDIOM is broken in Xcode 6.3+ as it is no longer a preprocessor define – k3a Apr 26 '15 at 14:44