Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to set (cornerRadius) user defined inspected property on (UIView)

Details

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:

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
        }
    }
}

Log

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)

Question

What is causing the application not to load User Defined Runtime Attributes that I added under Identity Inspector?

like image 914
E-Riddie Avatar asked Oct 29 '14 10:10

E-Riddie


2 Answers

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

enter image description here

Solution to fix-

  • Set correct class in interface Builder, in your case MyView
  • Check Designable status. It should be Up to date , if it's not than something is wrong.

enter image description here

Also If you decided to remove customer Designable you should

  • Remove custom class
  • Remove User Defined Runtime Attributes

enter image description here

like image 93
Kostiantyn Koval Avatar answered Oct 30 '22 19:10

Kostiantyn Koval


Based on the question title, a few people may come here with a slightly different problem (like I did). If you

  • added an @IBInspectible
  • and then deleted it in code

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.

enter image description here

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.

like image 23
Suragch Avatar answered Oct 30 '22 19:10

Suragch