Kevin Glier
Kevin Glier

Reputation: 1386

Preprocessor-IF doesn't work

I'm trying to check with Preprocessor-Ifs if the Device is an iPad. If it is an iPad, I want to define something Devicespecific, but for some reason I can't check in an PP-IF if a PP-Constant is true.

Maybe you got an idea?

#ifdef UI_USER_INTERFACE_IDIOM

    #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

#else

    #define IS_IPAD false

#endif



#if IS_IPAD

    #define WIDTH 768
    #define HEIGHT 1024

#else

    #define WIDTH 320
    #define HEIGHT 480

#endif

Upvotes: 2

Views: 1670

Answers (4)

Felix
Felix

Reputation: 751

This is my approach: you can use this in the header file

#define _IPAD ((__IPHONE_OS_VERSION_MAX_ALLOWED >= 30200) && (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad))
#define GUI_TITLE_LABEL_WIDTH    (_IPAD? 220*2 : 220)
#define UI_FONT_SIZE             (_IPAD? 20 : 16)

Short and easy :D

Upvotes: 2

Rayfleck
Rayfleck

Reputation: 12106

(When you ask a question on SO, you should tell what you tried, and what happened.) Anyway, I think you will not be able to do what you want there, because at compile time, the compiler does not know what device you will be running on. You could compile the code, and then run on an iPad, and iPhone, an iPod - how could the pre-processor possibly know which device you will be running on in the future?

Upvotes: 0

cutsoy
cutsoy

Reputation: 10251

Preprocessor rules are, (surprise, surprise) processed prior to building the app. Since it's an universal app, it doesn't yet know if it's running on an iPad or an iPhone.

Use this:

#ifdef UI_USER_INTERFACE_IDIOM
    #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#else
    #define IS_IPAD false
#endif

#define WIDTH (IS_IPAD ? 768 : 320)
#define HEIGHT (IS_IPAD ? 1024 : 480)

Upvotes: 11

Joe
Joe

Reputation: 57179

You have a runtime check inside of a #if statement. A preprocessor check will not evaluate (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) so the width and height will have to be set at runtime since you can not tell if it is an iPad until runtime. I would also recommend using 0 instead of false.

Upvotes: 0

Related Questions