未能设置(cornerRadius)用户定义的检查属性(UIView)

时间:2020-11-29 22:18:23

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.

我正在观看2014年WWDC上的会话411的视频,其中提到“接口构建器有什么新东西”,我正在尝试如何创建框架,以便让@IBDesignable类在无需运行应用程序的情况下预览我在故事板中的更改。

@IBInspectable properties are showing correctly when I add my class to a specific view and are rendering the view correctly with the below code:

当我将我的类添加到一个特定的视图并正确地呈现以下代码时,@IBInspectable属性将会正确显示:

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.

我注意到这些属性被添加到用户定义的运行时属性(在Identity Inspector下)。现在我期望的是,当我运行代码以保持我用特定视图做的更改时。

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?

是什么原因导致应用程序不加载我在Identity Inspector下添加的用户定义的运行时属性?

3 个解决方案

#1


38  

The code is correct.
When you declare a @ IBDesignable all the @IBInspectable properties are exposed to the Interface Builder as User Defined Runtime Attributes.

代码是正确的。当您声明@ IBDesignable时,所有的@ ibinspectable属性都将作为用户定义的运行时属性公开给接口构建器。

The problem -

问题- - - - - -

Unknown class MyClass in Interface Builder file

接口构建器文件中的未知类MyClass

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

Is意味着接口构建器无法找到某个类。你在你的应用程序中设置了错误的类,你的customView类是MyView,但在Interface Builder中你有MyClass。

未能设置(cornerRadius)用户定义的检查属性(UIView)

Solution to fix-

解决方案来解决,

  • Set correct class in interface Builder, in your case MyView
  • 在接口构建器中设置正确的类,在您的case MyView中。
  • Check Designable status. It should be Up to date , if it's not than something is wrong.
  • 检查可被识别的状态。它应该是最新的,如果它不是错的。

未能设置(cornerRadius)用户定义的检查属性(UIView)

Also If you decided to remove customer Designable you should

另外,如果您决定删除可设计的客户,您应该这样做

  • Remove custom class
  • 删除自定义类
  • Remove User Defined Runtime Attributes
  • 删除用户定义的运行时属性

未能设置(cornerRadius)用户定义的检查属性(UIView)

#2


21  

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

基于问题标题,一些人可能会带着稍微不同的问题来到这里(就像我一样)。如果你

  • added an @IBInspectible
  • 添加了一个@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].

未能在[您的自定义视图]上设置(xxx)用户定义的已检查属性…:这个类不是键值编码的关键[xxx]。

The solution is to delete the the old property.

解决方案是删除旧属性。

未能设置(cornerRadius)用户定义的检查属性(UIView)

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.

同样,这不是OP问题的答案,但它可能是其他人问题的答案。

#3


0  

in Indentity inspector add in user Defined runtime attributes

在Indentity检查器中,添加用户定义的运行时属性

layer.cornerRadius - type string

层。cornerRadius - string类型

未能设置(cornerRadius)用户定义的检查属性(UIView)

after go to Attibutes inspector and select "Clip Subviews"

转到Attibutes检查器并选择“Clip Subviews”

未能设置(cornerRadius)用户定义的检查属性(UIView)

#1


38  

The code is correct.
When you declare a @ IBDesignable all the @IBInspectable properties are exposed to the Interface Builder as User Defined Runtime Attributes.

代码是正确的。当您声明@ IBDesignable时,所有的@ ibinspectable属性都将作为用户定义的运行时属性公开给接口构建器。

The problem -

问题- - - - - -

Unknown class MyClass in Interface Builder file

接口构建器文件中的未知类MyClass

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

Is意味着接口构建器无法找到某个类。你在你的应用程序中设置了错误的类,你的customView类是MyView,但在Interface Builder中你有MyClass。

未能设置(cornerRadius)用户定义的检查属性(UIView)

Solution to fix-

解决方案来解决,

  • Set correct class in interface Builder, in your case MyView
  • 在接口构建器中设置正确的类,在您的case MyView中。
  • Check Designable status. It should be Up to date , if it's not than something is wrong.
  • 检查可被识别的状态。它应该是最新的,如果它不是错的。

未能设置(cornerRadius)用户定义的检查属性(UIView)

Also If you decided to remove customer Designable you should

另外,如果您决定删除可设计的客户,您应该这样做

  • Remove custom class
  • 删除自定义类
  • Remove User Defined Runtime Attributes
  • 删除用户定义的运行时属性

未能设置(cornerRadius)用户定义的检查属性(UIView)

#2


21  

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

基于问题标题,一些人可能会带着稍微不同的问题来到这里(就像我一样)。如果你

  • added an @IBInspectible
  • 添加了一个@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].

未能在[您的自定义视图]上设置(xxx)用户定义的已检查属性…:这个类不是键值编码的关键[xxx]。

The solution is to delete the the old property.

解决方案是删除旧属性。

未能设置(cornerRadius)用户定义的检查属性(UIView)

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.

同样,这不是OP问题的答案,但它可能是其他人问题的答案。

#3


0  

in Indentity inspector add in user Defined runtime attributes

在Indentity检查器中,添加用户定义的运行时属性

layer.cornerRadius - type string

层。cornerRadius - string类型

未能设置(cornerRadius)用户定义的检查属性(UIView)

after go to Attibutes inspector and select "Clip Subviews"

转到Attibutes检查器并选择“Clip Subviews”

未能设置(cornerRadius)用户定义的检查属性(UIView)