---
title: "#119: IBInspectable \u0026 IBDesignable 🖌"
url: https://littlebitesofcocoa.com/119-ibinspectable-ibdesignable
author: Jake Marsh
published: 2015-11-05
topics: ["UIKit","Techniques","Interface Builder"]
description: "Interface Builder is awesome. Assembling our interfaces in it can be a great way to work. Sometimes though, we want to create custom views that we draw and..."
---

# #119: IBInspectable & IBDesignable 🖌

By Jake Marsh, November 5, 2015. HTML: https://littlebitesofcocoa.com/119-ibinspectable-ibdesignable

_Interface Builder_ is awesome. Assembling our interfaces in it can be a great way to work. Sometimes though, we want to create custom **views** that we draw and manage ourselves in code. It'd be nice if these custom **views** could work just as well inside _Interface Builder_ as they do in our code. Today we'll look at two awesome features that debuted with **Xcode 6** called _@IBInspectable_ and _@IBDesignable_. Let's dive in:

``` swift
@IBInspectable var borderWidth: CGFloat = 0 {
  didSet { layer.borderWidth = borderWidth }
}
```
When crafting a custom **view**, we can make a property “inspectable” in _Interface Builder_ by prefixing its definition with _@IBInspectable_. Now, when we drag an instance of our custom **view** onto the **canvas** in _Interface Builder_ we'll be able to change our **properties** with native controls!

<img src='https://media.littlebitesofcocoa.com/bite-assets/119-ibinspectable-and-ibdesignable-inspectable-screenshot.png' style='max-width: 261px;' />

This technique even works with **extensions**! Tired of not being able to configure things like a **UIButton's** borders in _IB_? Let's add a tiny **extension** to **UIButton** to make it more <i>inspectable</i>:

``` swift
extension UIButton {
  @IBInspectable var borderWidth: CGFloat {
    get { return layer.borderWidth }
    set { layer.borderWidth = borderWidth }
  }
}
```

This is great so far, but what we <i>really</i> want is to be able to see the effects of our **property** edits <i>live</i> in the _IB_ **canvas**. We can prefix any view's definition with _@IBDesignable_ to tell _IB_ to render the **view**, live, right on the **canvas**.

``` swift
@IBDesignable
class SpeedometerView : UIView {
  var pieChartView: PieChartView

  @IBInspectable var meterColor: UIColor = UIColor.yellowColor() {
    didSet { pieChartView.segmentColor = meterColor }
  }

  // ...
```

When we combine the two techniques we unlock a much more intuitive (and fast) way to build custom **views**. We can even implement a special **function** called `prepareForInterfaceBuilder()` on our **view** where we can configure it with example data that will be drawn <i>only</i> when it's rendered in **IB**.

If anything goes wrong, we can debug our **view** by setting a **breakpoint** like normal, then choosing **Editor** > **Debug Selected Views** from **Xcode's** menu.
