简体   繁体   English

#define基于平台[iPhone或iPad]

[英]#define based upon platform [iPhone or iPad]

I'm trying to make my iPhone app compatible with the iPad. 我正在尝试让我的iPhone应用程序与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. 由于屏幕较大,我希望用于图像的一些常量在iPad上比在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]. 不幸的是,xcode 4无法编译这给出错误:'Token'[“在预处理器中无效..'[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. UI_USER_INTERFACE_IDIOMUIUserInterfaceIdiomPad不是预处理器。 They are part of iOS, so you should do: 它们是iOS的一部分,所以你应该这样做:

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 如果您计划支持3.2之前的iOS版本,请参阅此内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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