简体   繁体   中英

#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?

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..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM