开发者

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_US开发者_如何学PythonER_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


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)


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜