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
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 likeSo, 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!
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! :-)
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.
It is going to failed because the variables of
IBInspectable
are used in someone elseIBDesignable
classI stumbled on this issue as well, and have two suggestions that might be helpful:
#if targetEnvironment(simulator)
blocks to at least pass the build.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.