Xcode Runtime Attributes

The last 1% of pixels in an app, take 10% of the time.

Quickly changing a button to have rounded corners and a coloured border is still not completely possible in Interface Builder. The use of Runtime Attributes does make it easier though.

With Expanding User-Defined Runtime Attributes in Xcode with Objective-C, a few more uses have come to light.

The list of possible values is wider than I realised including Boolean, NSNumber, NSString, CGPoint, CGSize, CGRect, NSRange, and UIColor. And it’s the inclusion of UIColor that makes it really useful. As long as you’re happy using the color picker.

But not even UIColor can help with a button border color. For that you’ll need CGColor. Mike Woelmer has provided a CALayer+RuntimeAttribute category to help.

While this approach keeps the view controller code cleaner, there do seem be some inefficiencies in applying the same attributes to multiple controls.

As mentioned by Sam Dods in his Interface Builder tutorial, this approach may lead to future code maintenance issues if other developers don’t realise that runtime attributes can be set in this way. More reason to share this knowledge further.

This post has been written in the spirit of “Write about something you’ve learnt, when you’ve just learnt it.” – Ash Furrow, echoed by NatashaTheRobot.

This entry was posted in Development, iOS, Learning. Bookmark the permalink.