Making your circular views circular in Interface Builder
Tags: ios, interfacebuilder, xcode, uikit
Published: 2019-04-18 13:17:33

---
title: Making your circular views circular in Interface Builder
published: true
canonical_url: https://www.appsdissected.com/circular-view-interface-builder-ibdesignable/
tags: iOS, InterfaceBuilder, Xcode, UIKit
---

A really common thing to do in iOS interfaces is make interface elements either circular or into a squircle (squircular?). This is a common thing to do when displaying user avatars, for example.

If you’re building your interface in a storyboard, though, you’ll find one big problem when you add the code to make your view circular: <strong>Your storyboard doesn’t look like your app.</strong>

<img class="alignnone size-full wp-image-237" src="https://www.appsdissected.com/wp-content/uploads/2019/04/Screen-Shot-2019-04-08-at-19.08.21.png" width="386" height="716">

<strong>Ugly.</strong> This just won’t do.

Luckily, there is a really simple way to make your storyboard controls match your app controls.

<hr>

First step, we’ll start with the code that implements the circular view in the app itself. Setting the <code>cornerRadius</code> in <code>layoutSubviews()</code> is important because the frame of the view will change as a result of the layout process, and the corner radius size needs to be half of the frame size to make it circular.
<pre><code>class CircleView: UIView {

    // ...

    override func layoutSubviews() {
        super.layoutSubviews()
        if (frame.width != frame.height) {
            NSLog("Ended up with a non-square frame -- so it may not be a circle");
        }
        layer.cornerRadius = frame.width / 2
        layer.masksToBounds = true
    }

    // ...

</code></pre>
Second, when you add your view to your storyboard or XIB, set an Aspect Ratio constraint to make it 1:1 by setting the frame size with equal width and height, and then adding the constraint. This will enforce the square frame (as long as Auto Layout doesn’t break that constraint for some other reason).

<img class="alignnone size-full wp-image-239" src="https://www.appsdissected.com/wp-content/uploads/2019/04/Screen-Shot-2019-04-08-at-19.06.32.png" width="532" height="2006"><img class="alignnone size-full wp-image-236" src="https://www.appsdissected.com/wp-content/uploads/2019/04/Screen-Shot-2019-04-08-at-19.05.18.png" width="548" height="714">

Now, to tackle the square view problem:

<img class="alignnone size-full wp-image-235" src="https://www.appsdissected.com/wp-content/uploads/2019/04/Screen-Shot-2019-04-08-at-19.08.21-1.png" width="386" height="716">

You need one more bit: add <code>@IBDesignable</code> to your UIView declaration:
<pre><code>@IBDesignable class CircleView: UIView {
</code></pre>
And voilà:

<img class="alignnone size-full wp-image-238" src="https://www.appsdissected.com/wp-content/uploads/2019/04/DraggedImage.png" width="386" height="716">

While we talked about this specifically for making your circular view should up correctly in Interface Builder, this works anytime you create fully custom views, or Interface Builder’s visualization of a view doesn’t reflect everything you have implemented in code.

It works because <code>@IBDesignable</code> is telling Interface Builder is that it should call into the compiled version of your view to do the drawing and layout. With that simple annotation, Interface Builder used the custom <code>layoutSubviews()</code>, which applied the appropriate circular layer mask, which Interface Builder than displayed.

Use it to make your storyboards look that much closer to your real app’s interface.

<hr />

Did you like this tip? The next tip on <a href="https://www.appsdissected.com/manual-app-badge-tutorial/">adding manual app badges to your app</a> is already waiting for you. 

Or <a href="https://www.appsdissected.com/newsletter/">sign up to get every tip straight to your inbox</a>, plus newsletter-only tips and deeper dives on website articles and discounts on future product launches.

This post originally published at <a href="https://www.appsdissected.com/circular-view-interface-builder-ibdesignable/">Apps Dissected.</a>