A useful UIView addition via Categories (Objective-C, iOS)

One thing that makes me surprise even in 2011 is how many Objective-C developers completely ignore (or misuse) the power that Categories in Objective-C give to you. In other words, Categories allows you to do almost any sort of customization to any given class, especially those from the official SDK, which can be of huge help to save a lot of typing and making your code more readable.

One good example is view location and size handling, a task that every Objective-C developer does a lot. And I mean it. Objective-C can be extremely verbose to do some very simple and laboral tasks, like creating and positioning views on the screen, as well manipulating them. Take the following as example:

UIView *v = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 20, 10)];
v.backgroundColor = [UIColor redColor];
[self.view addSubview:v];

// Change only the X
CGRect frame = v.frame;
frame.origin.x = 90;
v.frame = frame;

// [....]

// Later, if we want to change the width, do it all again
CGRect anotherFrame = v.frame;
anotherFrame.size.width = 200;
v.frame = anotherFrame;

// And so on

While this is a practical, very common and working code, it can get repetitive and boring very fast, as the need to change any property of the view’s frame arrises, because we can’t simply do

v.frame.origin.x = 90;

as the compiler would complain. Instead, we first need to assign the frame property to a temporary variable, change it, and then assign it back to the target instance. Do this a series of times and you’ll be willing to pull your hair off. (I do). So how do we make it better? By using Categories, of course. What we want to accomplish is the following:

v.x = 90;

// .....
v.width = 200;

// And so on

As you can see, the different is that, instead of writing three lines of code for every single property we might want to change, we write just one. Below you find the Category code for this UIView addition:

@interface UIView (FrameAdditions)
@property float x;
@property float y;
@property float width;
@property float height;
@end

@implementation UIView (FrameAdditions)
-(float) x {
	return self.frame.origin.x;
}

-(void) setX:(float) newX {
	CGRect frame = self.frame;
	frame.origin.x = newX;
	self.frame = frame;
}

-(float) y {
	return self.frame.origin.y;
}

-(void) setY:(float) newY {
	CGRect frame = self.frame;
	frame.origin.y = newY;
	self.frame = frame;
}

-(float) width {
	return self.frame.size.width;
}

-(void) setWidth:(float) newWidth {
	CGRect frame = self.frame;
	frame.size.width = newWidth;
	self.frame = frame;
}

-(float) height {
	return self.frame.size.height;
}

-(void) setHeight:(float) newHeight {
	CGRect frame = self.frame;
	frame.size.height = newHeight;
	self.frame = frame;
}
@end

Save this code in any file, like UIView+FrameAdditions.(m|h), include it in your project, and you are good to go!

Comments are closed.