Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#define based upon platform [iPhone or iPad]

I'm trying to make my iPhone app compatible with the iPad. In a header file I set up some constants. Because of the larger screen I want some constants used for images to be larger on the iPad than on to the iPhone. I found some suggestions on the internet to accomplish this:

#if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define imgAmcWidth 656.0f
#define imgAmcHeight 36.0f
#else
#define imgAmcWidth    240.0f
#define imgAmcHeight   20.0f
#endif

This seems to satisfy my needs. Unfortunately xcode 4 fails to compile this giving an error: 'Token "[" is not valid in preprocessor..' [LLVM GCC 4.2]. What am I doing wrong?

like image 216
Ghislain Avatar asked May 21 '11 08:05

Ghislain


2 Answers

While probably not the most elegant solution but to prevent a major rewrite of the code I decided to use the following trick:

#define iPad    UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
#define imgAmcWidth         (iPad ? 639.0f : 240.0f)
// etc..
like image 119
Ghislain Avatar answered Oct 21 '22 08:10

Ghislain


UI_USER_INTERFACE_IDIOM and UIUserInterfaceIdiomPad are not preprocessor things. They are part of iOS, so you should do:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    <define your constants here>
} else {
    <define your constants here>
}

See also this if you plan to support iOS versions previous to 3.2

like image 5
sergio Avatar answered Oct 21 '22 08:10

sergio