IBDesignable Build Failed
Asked Answered
M

8

27

I have Created IBDesignable and IBInspectable custom class to give shadow and corner radius for view

But When I assign Designable class to view, I get Designable Build Failed

This is my code

import Foundation

import UIKit

@IBDesignable
class DesignableView: UIView {
}

@IBDesignable
class DesignableButton: UIButton {
}

@IBDesignable
class DesignableLabel: UILabel {
}

@IBDesignable
class DesignableTableView: UITableView {

}

extension UIView {

    @IBInspectable
    var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius
        }
        set {
            layer.cornerRadius = newValue
        }
    }

    @IBInspectable
    var borderWidth: CGFloat {
        get {
            return layer.borderWidth
        }
        set {
            layer.borderWidth = newValue
        }
    }

    @IBInspectable
    var borderColor: UIColor? {
        get {
            if let color = layer.borderColor {
                return UIColor(cgColor: color)
            }
            return nil
        }
        set {
            if let color = newValue {
                layer.borderColor = color.cgColor
            } else {
                layer.borderColor = nil
            }
        }
    }

    @IBInspectable
    var shadowRadius: CGFloat {
        get {
            return layer.shadowRadius
        }
        set {
            layer.shadowRadius = newValue
        }
    }

    @IBInspectable
    var shadowOpacity: Float {
        get {
            return layer.shadowOpacity
        }
        set {
            layer.shadowOpacity = newValue
        }
    }

    @IBInspectable
    var shadowOffset: CGSize {
        get {
            return layer.shadowOffset
        }
        set {
            layer.shadowOffset = newValue
        }
    }

    @IBInspectable
    var shadowColor: UIColor? {
        get {
            if let color = layer.shadowColor {
                return UIColor(cgColor: color)
            }
            return nil
        }
        set {
            if let color = newValue {
                layer.shadowColor = color.cgColor
            } else {
                layer.shadowColor = nil
            }
        }
    }
}

This is what I got

error

Marlee answered 22/12, 2017 at 6:44 Comment(5)
have you try to compile and run ?Crossbench
Yes I try to compile and run...but does not get actual outputMarlee
Check without assigning Designable class, and build, restart your xcodeNotice
Possible duplicate of When I set the image to IBImageView, get build failedSelmaselman
@RaviPanchal thx, worked for me. Face-palmed myself afterwards. :)Anesthetic
M
9

It was failing because the variables of IBInspectable are used in someone else's IBDesignable class

The following steps resolved the issue:

  1. Rename the class
  2. Clean the project.
  3. Select storyboard, go to the Editor menu and do Refresh All Views or else select Automatic Refresh view; wait for build to be completed.
Marlee answered 22/12, 2017 at 10:34 Comment(1)
What do you mean? - I mean, what if the component is reusable like, a cart that has a shadow and it is all over the place. Should it build with an error because IBDesignables are not meant for reusability?Fauman
M
51

First Try: IBDesignable Build Failed

For me when I was hovering over the InterfaceBuilder Designables: Build Failed, it was giving an error message saying something like

Cannot find any source files for the declaration or ...

So, I got it as a clue like that Xcode couldn't index my custom UIView class file so what I did is I just quit Xcode and Restarted it and then it had indexed my custom Swift class file and InterfaceBuilder was able to find it properly.

First of all try it out and then go to other options!

Meurer answered 27/6, 2018 at 18:21 Comment(5)
Hovering it is!Doorn
Still happening (same issue, same diagnostic, same fix) with Xcode 10.0.Gretagretal
oh my god, when will this stop happen?Titlark
@DimaGimburg not any sooner I think. These kinds of bugs endure for so long in Xcode in my experience. Btw, Merry Christmas! :DMeurer
I've tried the above and the issue is still persisting.Impossible
M
9

It was failing because the variables of IBInspectable are used in someone else's IBDesignable class

The following steps resolved the issue:

  1. Rename the class
  2. Clean the project.
  3. Select storyboard, go to the Editor menu and do Refresh All Views or else select Automatic Refresh view; wait for build to be completed.
Marlee answered 22/12, 2017 at 10:34 Comment(1)
What do you mean? - I mean, what if the component is reusable like, a cart that has a shadow and it is all over the place. Should it build with an error because IBDesignables are not meant for reusability?Fauman
A
7

In my case, the designables were working fine in my storyboard until I deleted a few labels in the view that were no longer necessary to my user interface.

There were no useful messages in either the build log or in the DiagnosticReport folder.

The fix for me was to delete the DerivedData folder (an Xcode "reboot") and rebuild the project. Magically, the storyboard is again showing my custom knobs! :-)

Astonied answered 17/3, 2018 at 1:8 Comment(1)
In my case, simply cleaning the project and restarting XCode worked.Stilwell
P
7

I stumbled on this issue as well, and have two suggestions that might be helpful:

  1. In the "Report Navigator" (cmd-9) check the "Interface Builder" build log, it's a separate build from your normal project build, there could be useful error messages there instead of just "Build failed".
  2. The Interface Builder build is done for the Simulator, i.e. x86_64 architecture. Some parts of iOS (like Metal in my case) are not present on the simulator, resulting in build failures. I defined some classes and functions inside #if targetEnvironment(simulator) blocks to at least pass the build.
Pyongyang answered 14/7, 2018 at 14:27 Comment(0)
B
0

I struggled with this too and then deleted projects derived data and restarted Xcode before it would work again. "Debug Selected Views" was always disabled and designable build was always showing as failed. App compiled and ran fine.

Brookebrooker answered 18/6, 2018 at 1:3 Comment(0)
P
0

If your CustomClass is added in to the project as Reference files then this occurs. That is for example you can see Blue coloured folder in the Project Explorer, that are Reference folders. If you are copying the Classes into our project then make sure that 'Copy items if needed' is selected.

In my case, this was the issue.

Petrosal answered 22/8, 2018 at 12:27 Comment(0)
L
0

Even with the first 2 steps fixed mine. 1. Set the Class to DesignableView. 2. Set the Module to 3. Clean the build. 4. Restart Xcode.

Lorrin answered 1/10, 2018 at 6:31 Comment(0)
R
0

Make sure you have entered your development team in Project settings -> Signing and Capabilities tab. You cannot use @IBDesignable without development team. However it's enough to use your personal team - only AppleID required.

Ruddock answered 25/5, 2021 at 18:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.