Friday, 3 October 2014

How to use User Defined Runtime Attributes in Xcode Interface Builder

What is the mean by “User Defined Runtime Attributes” ?

“User Defined Runtime Attributes” are featured from Xcode 4 and iOS 5,  great feature supported in interface builder. But bad thing is that most of the intermediate developers are not aware about it.

Where can I found in Interface builder ?
take a look under Identity Inspector tab.

User Defined Runtime Atrributes screen in interface builder

Key Path- Its like properties of controls and or setter methods.
Value- Assigned values to those properties and or setter methods.
What about the 'Type' ?

Below are the supported Attributes types 

User Defined Runtime Atrributes Supported types


Lets come to the point.

How can we  use these in our application.
It is not possible to set a View's CornerRadius or borderWidth using interface builder's Attributes Inspector. To achieve this we unnecessarily creates a IBOutlet and use CALayer for this.


    _myBGview.layer.borderWidth = 3;
    _myBGviewlayer.cornerRadius = 15;

Lets do the same using Interface builder

Configure User Defined Runtime attributes in interface builder

Wow its so simple Just fill out the objects property and select type and set the value

One problem is here that I required to set the color of the border but CALayer's color is CGColorRef type not UIColor type

So we can use a trick of Objective-C's Category. Just implement a setter method.

@implementation CALayer (Enhancement)

     self.borderColor = mycolor.CGColor;

Configure User Defined Runtime attributes of unsupported types in interface builder

 Its all about Using User Defined Runtime Attributes from my side. If  you have any other best way to configure User Defined Runtime Attributes in Interface builder
 Please share using comments section.
It would be much appreciated.

No comments:

Post a comment