Failed to set () user defined inspected property on (UIButton)

后端 未结 6 1208
青春惊慌失措
青春惊慌失措 2021-01-17 09:50

In a simple ViewController, I added one UIButton. I would like to use \"User Defined Runtime Attributes\". For now I added the default Bool attribute.

The b

相关标签:
6条回答
  • 2021-01-17 10:08

    Another way to set the set a property value in the Interface Builder is that create an IBOutlet of your view on your view controller.

    @IBOutlet weak var yourView: UIView!
    
    
    yourView.layer.shadowOffset = CGSize(width: 0, height: 1)
    yourView.layer.shadowColor = UIColor.lightGray.cgColor
    yourView.layer.shadowOpacity = 1
    yourView.layer.shadowRadius = 5
    yourView.layer.masksToBounds = false
    yourView.layer.cornerRadius = 20
    
    0 讨论(0)
  • 2021-01-17 10:13

    Compare @IBInspectable(in sub class) and Key Path(identity inspector). Remove key value which is not in @IBInspectable.

    0 讨论(0)
  • 2021-01-17 10:18

    Looks like failed to set keyPath bool value to true of false. setKeyPath to true or false!

    0 讨论(0)
  • 2021-01-17 10:24

    Since this thread came up about 5 times in my search for a solution, I wanted to post my fix here for future frustrated souls. The reason I kept getting this error is because Interface Builder doesn't clean up old IBInspectable properties if you're experimenting with them.

    In my case, I started with an Int property called type and set a few custom buttons on my screen to use a value of 3 as a test. Later, I changed it to a more meaningful property name and made it a string, then set a few of those. But when I ran the app, I was getting this message.

    I didn't discover the error until I went to the Identity Inspector and noticed that I still had an old keypath and value in use for some of the buttons:

    Removing the old keypath fixed the problem.

    0 讨论(0)
  • 2021-01-17 10:26

    Well, your button doesn't have a property called keyPath to be setting it to something.

    User Defined Runtime Attributes are used to simply set a property value in the Interface Builder.

    You can use a standard property that every UIButton has, for example backgroundColor:

    Or, you can create a custom UIButton subclass, add a property to it, then set the button's class to the created custom subclass and set the property value in the User Defined Runtime Attributes section.

    You can check out, for example, this answer, it contains an example of a custom class with User Defined Runtime Attributes: https://stackoverflow.com/a/24433125/3445458

    Edit: make sure you are not using optional (or implicitly unwrapped optional) for the type, it doesn't work with User Defined Runtime Attributes (I guess because the Interface Builder doesn't know whether the value can be set to nil, and so show it in the options or not).

    So you can't do

    var keyPath: Bool!
    

    instead you can only do

    var keyPath: Bool = false
    

    or don't use a default value and set it in the constuctor instead. For some reason it works with an optional String (and in the example they use an optional String), but it doesn't with optional Bool. To conclude, don't use or worry about User Defined Runtime Attributes too much, it is clearer to set the default values in code!

    Hope this helps! Good luck!

    0 讨论(0)
  • 2021-01-17 10:33

    In User Defined Runtime Attributes use layer.cornerRadius instead of cornerRadius.

    0 讨论(0)
提交回复
热议问题