0

How can I set a User Defined Runtime Attribute in Xcode to a value associated with the object? For example, I want to set the layer.cornerRadius of a view to always be size.width / 2 to round the corners. I know that if I know the size of the view, I can set it to a constant like this:

enter image description here

Is there a way to do this dynamically? I have tried replacing 150 with size.width / 2 but I get the following warning:

The value “size.width / 2” is invalid.

Please provide a valid value.

Community
  • 1
  • 1
Ben Botvinick
  • 2,837
  • 3
  • 16
  • 41
  • You would have to this in `viewDidLoad()` or `didLayoutSubviews()`, as far as I know. – Chris Jul 29 '18 at 21:15
  • the corresponding property type of NSNumber: Number – NSNumber * or any numeric scalar, e.g. NSInteger. May check https://stackoverflow.com/questions/30089193/what-is-key-path-in-user-defined-runtime-attributes#37851027 – black_pearl Jul 30 '18 at 01:31

1 Answers1

0

You should add under the viewDidLoad() method, the following:

yourView.layer.cornerRadius = yourView.frame.width / 2
Fayyouz
  • 682
  • 7
  • 18