ios 无法在(UIView)上设置(cornerRadius)用户定义的检查属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26627789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:24:48  来源:igfitidea点击:

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

iosswiftios8interface-builder

提问by E-Riddie

Details

细节

I was watching the video of Session 411 on WWDC 2014referring to "What's new on Interface Builder", and I was trying out how to create Frameworks in order to make @IBDesignableclasses to preview my changes in Storyboard without having to run the application.

我正在观看WWDC 2014 上 Session 411的视频,其中提到了“ Interface Builder 的新功能”,我正在尝试如何创建框架,以便让@IBDesignable类在 Storyboard 中预览我的更改,而无需运行应用程序。

@IBInspectableproperties 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 Attributesthat I added under Identity Inspector?

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

回答by Kostiantyn Koval

The code is correct.
When you declare a @ IBDesignableall the @IBInspectableproperties are exposed to the Interface Builder as User Defined Runtime Attributes.

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

The problem-

问题——

Unknown class MyClass in Interface Builder file

Interface Builder 文件中的未知类 MyClass

Is means that Interface Builder couldn't find some class. You have set wrong classthat doesn't exist in your app.
Your customView class is MyViewbut in the Interface Builder you have MyClass

Is 意味着 Interface Builder 找不到某个类。您设置了应用程序中不存在的错误
您的 customView 类是MyView但在您拥有的 Interface Builder 中MyClass

enter image description here

在此处输入图片说明

Solution to fix-

解决方案-

  • Set correct class in interface Builder, in your case MyView
  • Check Designablestatus. It should be Up to date, if it's not than something is wrong.
  • 在您的情况下,在界面生成器中设置正确的类 MyView
  • 检查可设计状态。它应该是最新的,如果不是错误的话。

enter image description here

在此处输入图片说明

AlsoIf you decided to remove customer Designable you should

此外,如果您决定删除客户 Designable,您应该

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

enter image description here

在此处输入图片说明

回答by Suragch

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
  • 添加了@IBInspectible
  • 然后在代码中删除它

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.

解决方法是删除旧属性。

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.

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

回答by Pablo Ruan

in Indentity inspector add in user Defined runtime attributes

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

layer.cornerRadius- type string

layer.cornerRadius- 输入字符串

first

第一的

after go to Attibutes inspector and select "Clip Subviews"

转到属性检查器并选择“剪辑子视图”后

second

第二