I was watching the video of Session 411 on WWDC 2014 referring to "What's new on Interface Builder", and I was trying out how to create Frameworks in order to make @IBDesignable
classes to preview my changes in Storyboard without having to run the application.
@IBInspectable
properties are showing correctly when I add my class to a specific view and are rendering the view correctly with the below code:
@IBDesignable
class MyView: UIView {
@IBInspectable var borderColor: UIColor = UIColor.clearColor() {
didSet {
layer.borderColor = borderColor.CGColor
}
}
@IBInspectable var borderWidth: CGFloat = 0 {
didSet {
layer.borderWidth = borderWidth
}
}
@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadius
}
}
@IBInspectable var masksToBounds: Bool = false {
didSet {
layer.masksToBounds = masksToBounds
}
}
}
I noticed that these attributes were being added on User Defined Runtime Attributes (Under Identity Inspector). Now what I am expecting from this is when I run the code to keep these changed I made with a specific view.
I run the application and the view doesn't load the User Defined Runtime Attributes, and gives this error on output (not crash):
Unknown class MyClass in Interface Builder file
Failed to set (cornerRadius) user defined inspected property on (UIView)
What is causing the application not to load User Defined Runtime Attributes that I added under Identity Inspector?
The code is correct.
When you declare a @ IBDesignable
all the @IBInspectable
properties are exposed to the Interface Builder as User Defined Runtime Attributes.
The problem -
Unknown class MyClass in Interface Builder file
Is means that Interface Builder couldn't find some class. You have set wrong class that doesn't exist in your app.
Your customView class is MyView
but in the Interface Builder you have MyClass
Solution to fix-
MyView
Also If you decided to remove customer Designable you should
Based on the question title, a few people may come here with a slightly different problem (like I did). If you
Then you may also get an error similar to
Failed to set (xxx) user defined inspected property on [Your Custom View] ...: this class is not key value coding-compliant for the key [xxx].
The solution is to delete the the old property.
Open the Identity inspector for your class, select the property name under User Defined Runtime Attributes, and press the minus button (-).
Again, this is not the answer to the OP's question, but it might be the answer to someone else's problem who comes here.
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