The tedium of constantly rebuilding the app to see your changes has been a long held frustration for many. Now, imagine that you can draw CoreGraphics code in Interface Builder without building the app. Could it be so? Will this nightmare end? Yes, yes it will.
Since Xcode 6 launched, the sun has risen on a whole new world. So we dived right in and sent Jose to explore. Here are his findings:
Let me explain... These annotations help us to modify and visualize the changes of the view from the interface builder, but crucially without building the app.
Let’s think about some attributes predefined in Xcode: changing the background and the tint colours, the alpha component of the view, etc.
Changing any of these properties is automatically reflected in our xib file on Interface Builder without doing extra work, so you could match IBInspectable as customised attributes like the image alongside.
This attribute has been available since Xcode 6 and can be used for both Objective-C and Swift. It applies to the class as a prefix, and it lets Interface Builder know that it should render the view directly in the canvas.
That means that you will see how your custom views will appear without building and running your app after each change.
Depending if you program in Objective-C or Swift, you have to change the way you add as in the image to the left.
Every time you open the Interface Builder, Xcode quickly compiles the nib. This uses the initialisers and the drawing methods to update the xib in real time.
IBDesignable and IBInspectable are related to each other, so while the first one applies to the class definition, the second one applies to the properties.
Once you set the IBInspectable prefix to the class, you can declare some properties with the IBInspectable prefix. These properties will allow you to change the outlet class in real time. Unfortunately, you can’t modify everything, only a few types can be set to IBInspectable. Here is the full list:
Though it is limited, that list can still help considerably. You only need to prefix the properties with the IBInspectable keyword and this will automatically add the properties in our Attributes Inspector.
As before it depends, of course, if we program in Objective-C or Swift. What Xcode does is to set those IBInspectable properties as User Defined Runtimes Attributes, as you can see in the screenshot of the Identities Inspector.
As you may guess, you'll need to do something else with the IBInspectable properties - you need to let the view know what we want to do with them. If you want to set a background colour with the IBInspectable property, you need to assign it to the backgroundColor property of the view.
The question then follows…
You can set it in some places: initializers, drawing methods and (if you program with Swift) in the didSet method of the IBInspectable var:
Once you've finished setting the assignments, open the Interface Builder and switch to the Identity Inspector.
Then you should see the following:
That means that you've have finished the set up and then can easily modify the properties to view the changes.
You can use CoreGraphics as well to draw automatically in the Interface Builder. This saves time and storage. Moreover you can easily change the properties by using IBInspectable properties. Look at the following example to see an approach of what you can do. Keep in mind that you're using CoreGraphics, not images. Not bad eh?
Fortunately, PaintCode generates a class with the drawing methods that helps a lot with the drawing as you'll only need to set that method in the drawRect method.
You can also use this to draw the CoreGraphics canvas automatically with IBDesignable and IBInspectable, just follow these steps:
That allows you to see the results in your IB without running the app! You can easily change more properties by using out IBInspectables. In the example above you'll see I’m changing the canvas colour as well. Internally this is a set colour that is being called for every canvas with setFill.
Check out our example project. It was made for Objective-C and you simply need to open the xib file, go to the attributes inspector and have a play around to see the results.
This article was originally written for Brightec by Jose Martinez
Search over 200 blog posts from our team
Subscribe to our monthly digest of blogs to stay in the loop and come with us on our journey to make things better!