I'm always programmatically creating and laying out UI elements in multiple ViewControllers and I've wanted an easy way to get relevant Screen information (sizes/points) globally using a helper class.
I've created a FlexibleScreen class and would like some constructive feedback. In addition to the typical code review feedback of format, syntax, etc I would really like to know how well this solves my problem functionally and what suggestions you have about the design or what additional functionality I could add.
FlexibleScreen.h
#import <Foundation/Foundation.h>
@interface FlexibleScreen : NSObject
+(CGSize)getScreenSize;
+(CGPoint)getCenter;
+(CGPoint)getCenterWithY:(double)y;
+(CGPoint)getCenterWithX:(double)x;
@end
FlexibleScreen.m
#import "FlexibleScreen.h"
@interface FlexibleScreen()
@property (assign) CGRect bounds;
@end
@implementation FlexibleScreen
+(CGSize)getScreenSize
{
return CGSizeMake([[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
}
+(CGPoint)getCenter
{
return CGPointMake([[UIScreen mainScreen] bounds].size.width/2, [[UIScreen mainScreen] bounds].size.height/2);
}
+(CGPoint)getCenterWithY:(double)y
{
return CGPointMake([[UIScreen mainScreen] bounds].size.width/2, y);
}
+(CGPoint)getCenterWithX:(double)x
{
return CGPointMake(x, [[UIScreen mainScreen] bounds].size.height/2);
}
@end
Implementing Code
#import "FlexibleScreen.h"
...
CGRect headerFrame = CGRectMake(0, 0, [FlexibleScreen getScreenSize].width, HEADER_HEIGHT);
CGRect footerFrame = CGRectMake(0, [FlexibleScreen getScreenSize].height-FOOTER_HEIGHT, [FlexibleScreen getScreenSize].width, FOOTER_HEIGHT);
...
[label setCenter:[FlexibleScreen getCenterWithY:0]];
UPDATE: As I used this more I realized I wanted it to be a lot simpler. Here is my latest version GitHub: UIScreen+Flexible.