Press "Enter" to skip to content

Useful Objective-C Macro for iOS

Author 0

 

As a developer, I always adjust my macro for the new device like iPhone X and test it before adding to my projects. Keep your all macros in a .h file is a good practice, though you have your own choices here, go for what you like and feel comfortable with. I usually name it Domain.h and include Domain.h wherever I need it.

These are some useful macros, I keep in my projects.

Domain.h

#define CURRENT_OS() ([[[UIDevice currentDevice] systemVersion] intValue])
#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)
#define IS_ZOOMED (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)

#define SCREEN_WIDTH() ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT() ([UIScreen mainScreen].bounds.size.height)

#define IS_IPHONE4 (([[UIScreen mainScreen] bounds].size.height-480) ? NO:YES)
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568) ? NO:YES)
#define IS_IPHONE6 (([[UIScreen mainScreen] bounds].size.height-667) ? NO:YES)
#define IS_IPHONE6PLUS (([[UIScreen mainScreen] bounds].size.height-736) ? NO:YES)

#define IS_IPAD ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH(), SCREEN_HEIGHT()))
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH(), SCREEN_HEIGHT()))

#define IS_IPHONE_5_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH <= 568.0)
#define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
#define IS_IPHONE_6_7_8 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
#define IS_IPHONE_6P_7P_8P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)
#define IS_IPHONE_X (IS_IPHONE && SCREEN_MAX_LENGTH == 812.0)

 

More Macros for us…

Useful Objective C Macro from Amattn

http://amattn.com/p/useful_objective_c_macros.html

My Favorite Macros for Objective-C Development in Xcode from Matt

http://matt.coneybeare.me/my-favorite-macros-for-objective-c-development-in-xcode/