Reputation: 5510
I would like to know how to create an application wide (global) set of variables for detecting the current screen size?
For instance, at the moment, I do this in each class I create.
//.h
@property (assign) CGRect screenRect;
@property (assign) CGFloat screenWidth;
@property (assign) CGFloat screenHeight;
//.m
@synthesize screenRect;
@synthesize screenWidth;
@synthesize screenHeight;
screenRect = [[UIScreen mainScreen] bounds];
screenWidth = screenRect.size.width;
screenHeight = screenRect.size.height;
But I would like it where I could just call screenWidth or height from any class and not have to define them every time.
Upvotes: 0
Views: 44
Reputation: 53142
There are many ways, here's one:
Create a Header File called something like Constants.h and add this:
#define ScreenWidth ((int) [UIScreen mainScreen].bounds.size.width)
#define ScreenHeight ((int) [UIScreen mainScreen].bounds.size.height)
then wherever you want to use it
#import "Constants.h"
Use like so:
NSLog(@"The ScreenWidth Is: %i", ScreenWidth);
NSLog(@"The ScreenHeight Is: %i", ScreenHeight);
Upvotes: 1