Last time I've written about Behaviours in objc.io Architecture Series.
Xcode 6 has some great improvements, few people realise that IBInspectable works on any class, not just views.
Let's use that to improve upon my original Behaviours.
My behaviours are built on top of 3 principles:
- Use of runtime attributes for setting properties
- Reversing lifetime notation
- Generating events
Both 2. and 3. were easy to use since they were powered by visual binding, it was only a matter of connecting proper outlets/actions.
Using runtime attributes was far from ideal:
- Error prone - since you need to enter keyPath correctly, spelling mistakes will lead to crash when deserialising Xib/Storyboard which gets annoying.
- Need to read documentation / header to know what keyPaths are supported.
Enter Xcode 6
With introduction of Xcode 6 we’ve received live rendering of custom views (finally!?), to support custom views rendering you need to:
- Put your view classes inside a separate framework
- Specify IBDesignable attribute for your class interface
- For each configurable custom property you should specify IBInspectable attribute
If we add few IBInspectables like so:
Interface builder will now generate WYSIWYG editors for them:
This is great improvement to workflow, especially if you look at how many different types of properties receive their own editors:
This is great improvement to workflow, especially when I work with designers, I no longer need them to read docs for each custom view we have in app, they can just use visual editor 100% of time!
How does that help with Behaviours?
Very few people realised that IBInspectable can work without IBDesignable, and it get’s better:
IBInspectable works without needing frameworks, on any class.
That means that each Behaviour property can be made IBInspectable and you get all the benefits of WYSIWYG editor for no cost, how great is that?
Finally my Parallax behaviour get a nice visual editor:
Supporting older Xcode?
Xcode 6 is still beta and so are inspectables, fortunately for us Apple did great job of joining Runtime attributes and inspectables together:
If you have runtime attribute set for a specific keyPath that’s also IBInspectable property, it will show up as the value of that Inspectable
To be able to compile your IBInspectable enabled Behaviours on older Xcode 5 you just need to add empty macro for IBInspectable, like so:
All behaviours will now work as expected, you can use runtime attributes in Xcode 5 or Inspectable’s in Xcode 6.