I'm trying to give all my views a new property called newProperty
.
Since all the views would have it it would make more sense to add it to a super class.
In this case I want to add newProperty
to UIView
so that all UIView
subclasses (UIWindow
, UIPickerView
, UITabBar
, etc...) will inherit it.
So is it possible?

- 3,721
- 7
- 36
- 50
2 Answers
When your property needs a backing instance variable, you could use an associated object:
How do I use objc_setAssociatedObject/objc_getAssociatedObject inside an object?
What is objc_setAssociatedObject() and in what cases should it be used?
http://darkdust.net/writings/objective-c/attaching-objects
...but you may want to think this through before putting it to use.
When your property requires no instance variable, see Luke's answer.
Write a category on UIView.
In Xcode, go File -> New -> File and then select "Objective-C Category" from the Cocoa Touch row in the iOS section. Ensure the category is on UIView, give it a name and you're away.
From here, you can add properties or even instance methods, which, assuming you import the category in your app's prefix file, all classes that inherit from UIView will have access to.
EDIT: I have not worked with associated objects. This is a really simple example using pre-defined properties that already exist in the object, using my own properties to simplify their retrieval.
The OP has not stated what kind of property they wish to add to UIView, which is why I made the assumption that a category would suffice. I am certain you are all correct with the comments below re: data storage on a truly custom property.
I do not appreciate downvotes when in fact the objective of the OP has not been made quite so clear, regarding the usage of their "newProperty."
eg: Category on UIView called Position. (UIView+Position)
UIView+Position.h
@interface UIView (Position)
-(CGFloat)left;
-(CGFloat)right;
-(CGFloat)top;
-(CGFloat)bottom;
@property (nonatomic) CGFloat left;
@property (nonatomic) CGFloat right;
@property (nonatomic) CGFloat top;
@property (nonatomic) CGFloat bottom;
@end
UIView+Position.m
#import "UIView+Position.h"
@implementation UIView (Position)
-(CGFloat)left
{
return self.frame.origin.x;
}
-(CGFloat)right
{
return self.frame.origin.x + self.frame.size.width;
}
-(CGFloat)top
{
return self.frame.origin.y;
}
-(CGFloat)bottom
{
return self.frame.origin.y + self.frame.size.height;
}
@end

- 11,426
- 43
- 60
- 69
-
1A category can add methods to the class, but not a property. This is because a category cannot add a backing store / ivar for the getter and setter to manipulate. – Jeff Wolski May 22 '13 at 11:00
-
1You cannot add `iVars`. Refer : http://stackoverflow.com/q/6162589/667586 http://stackoverflow.com/a/6162619/667586 – Abhishek Bedi May 22 '13 at 11:01
-
1this would allow you to declare and define instance methods (including property declarations), but it does not permit storage (e.g. it is an error to synthesize a property in the implementation of a category). – justin May 22 '13 at 11:01
-
I have a category on UIView which adds a number of properties. I have also written the setters. I have no problems using them. – Luke May 22 '13 at 11:03
-
1@Luke: Show soime code buddy ? Did you use associative references? – Abhishek Bedi May 22 '13 at 11:05
-
1You could add mehtods that act as accessors. But you still need to find a place to store the data. And make sure the data is cleared when the object is deallocated. – Hermann Klecker May 22 '13 at 11:06
-
1@Luke what you have likely done is simply added methods -- without introducing additional storage (for ivars). adding methods is certainly doable within a category (and a property declaration declares methods and type but is not responsible for allocating storage). when a property is declared, it usually means that the property is backed by an ivar -- so i recommended associated objects. when the ivar is not needed, the methods-only-via-category approach is a good way to go -- i'm going to upvote it for that reason. – justin May 22 '13 at 11:12
-
edited upon request. seems we need some more info from the OP to be truly sure of their intentions. – Luke May 22 '13 at 11:16