Dmitry
Dmitry

Reputation: 1041

#ifdef macros for versions controlling

I use macros to differ the versions but I can't force it to work properly. I used:

#ifdef _IPHONE_4_0
  [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
 #else
  [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
 #endif

and

#if __IPHONE_OS_VERSION_MAX_ALLOWED < _IPHONE_4_0
  [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
 #else
  [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
 #endif

and

#if defined(__IPHONE_4_0)
  [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
 #else
  [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
 #endif

No matter what version I use - always called only one of the lines. And the __IPHONE_4_0 is always defined. Any ideas?

Upvotes: 1

Views: 1802

Answers (2)

Rajendra Prabhu
Rajendra Prabhu

Reputation: 1

A small change -

The following code should work:

 #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_4_0   
       [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES]; 
 #else   
       [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
 #endif

Plz note it's __IPHONE_4_0 not _IPHONE_4_0

Upvotes: -1

kennytm
kennytm

Reputation: 523474

The #if… processor directives are resolved at compile time. As long as you compile for the 4.0 SDK, the 4.0 variant will always be chosen.

If you intend to make the app works for < 4.0, you should use a runtime check:

UIApplication* app = [UIApplication sharedApplication];
if ([app respondsToSelector:@selector(setStatusBarHidden:withAnimation:)])
  [app setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
else
  [app setStatusBarHidden:YES animated:YES];

Upvotes: 2

Related Questions