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
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!
It was failing because the variables of IBInspectable
are used in someone else's IBDesignable
class
The following steps resolved the issue:
I stumbled on this issue as well, and have two suggestions that might be helpful:
#if targetEnvironment(simulator)
blocks to at least pass the build.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With