Skip to content

Reusable, Interface Builder designed views, done right

License

Notifications You must be signed in to change notification settings

andrebraga/IBReusable

Repository files navigation

IBReusable

Xcode 4 removed support for Xcode 3 plugins, including visual components. Thankfully, Xcode 6 mostly brought them back when IB_DESIGNABLE was introduced. Unfortunately most of the posts on Stack Overflow and several blogs are suggesting extremely complicated approaches (inserting a content view as a subview, or doing gymnastics on -layoutSubviews) for something that should be trivial and built into Interface Builder, namely (re)using views designed as XIBs on storyboards (and other XIBs). Well, if you squint a little, it already is 😜 but the documentation is not self-evident. Not anymore: this (spoiler alert: using -awakeAfterUsingCoder:) is The Right Way™ to do it :bowtie:

The actual code is implemented as a category in case you can't subclass IBReusableView (for instance if your are subclassing UIButton). Just use IBReusableView as guidance and adapt for your needs; the code is deceptively simple.

Example

On the left you can see the component being designed as a XIB, and on the right you find it reused as many times as you wish. Whatever is set as the parameters of IB_INSPECTABLE properties become the defaults when dragging and dropping on the Storyboard and other XIBs.

It is as simple as dragging a blank UIView into the canvas and changing its class.

Sample screenshot of component reuse

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

ARC is required, Xcode 4.4+ is required. Xcode 6 nullabilily attributes were used but could be stripped out or defined away if not supported by your compiler version.

It is probably compatible with any system version that supports ARC and compiler that supports literal array syntax. Other than guarded autolayout calls that are all iOS 6.0 compatible, only APIs introduced in iOS 4.0 are used.

Installation

IBReusable is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "IBReusable"

Author

André Braga, [email protected]

License

IBReusable is available under the MIT license. See the LICENSE file for more info.

About

Reusable, Interface Builder designed views, done right

Resources

License

Stars

Watchers

Forks

Packages

No packages published