IBDesignable Build Failed

2019-03-18 05:58发布

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

7条回答
Lonely孤独者°
2楼-- · 2019-03-18 06:21

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!

查看更多
We Are One
3楼-- · 2019-03-18 06:21

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! :-)

查看更多
forever°为你锁心
4楼-- · 2019-03-18 06:30

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.

查看更多
家丑人穷心不美
5楼-- · 2019-03-18 06:31

It is going to failed because the variables of IBInspectable are used in someone else IBDesignable class

  • I gave different name to my class
  • clean project .
  • then Select storyboard go to Editor menu and do Refresh All Views or else select Automatic Refresh view; wait for build to be completed
查看更多
兄弟一词,经得起流年.
6楼-- · 2019-03-18 06:33

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.
查看更多
【Aperson】
7楼-- · 2019-03-18 06:35

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.

查看更多
登录 后发表回答