Reputation: 2809
I want dynamically define a constant based on the different device heights. I tried to use this code but it doesn't work:
#define isPhone568 ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568)
#ifdef isPhone568
#define kColumnHeightPortrait568 548
#else
#define kColumnHeightPortrait568 (IS_IPAD ? 984 : 460)
#endif
Even if i'm using the 3.5" simulator, i get 548. What's wrong with this?
Upvotes: 1
Views: 167
Reputation: 11
The #ifdef
is used to check whether a macro is defined. As you define isPhone568
in the first line, #ifdef isPhone568
will be true.
If you want to test the value of an expression rather than the existance of a macro, you should use #if
instead. But #if
can test no more than the simple arithmetic expression, just as paxdiablo mentioned, "You can't run code in macro definitions".
Upvotes: 1
Reputation: 881983
You can't run code in macro definitions, it's a simple text substitution process that happens at compile-time. Hence you have no idea what the device characteristics are at that point, because you're not on the target device.
If you want to use something like [UIDevice currentDevice] userInterfaceIdiom
, you have to evaluate it at run-time, not in a compile-time macro, something like:
int kColumnHeightPortrait568 = 548;
if (([[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPhone)
|| ([UIScreen mainScreen].bounds.size.height != 568))
{
kColumnHeightPortrait568 = (IS_IPAD ? 984 : 460);
}
Upvotes: 2