Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IBDesignable Build Failed

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

like image 300
VishalPethani Avatar asked Dec 22 '17 06:12

VishalPethani


3 Answers

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!

like image 94
Randika Vishman Avatar answered Nov 09 '22 03:11

Randika Vishman


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.
like image 45
VishalPethani Avatar answered Nov 09 '22 04:11

VishalPethani


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.
like image 22
Jan Misker Avatar answered Nov 09 '22 03:11

Jan Misker