0

I have a class to use with @IBInspectable, to get the properties within my storyboard. Here is a small chunk of it:

/// UIView subclass to allow creating corners, shadows, and borders in storyboards.
final class GEView: UIView {

    // MARK: - Rounded corners
    @IBInspectable
    var cornerRadius: CGFloat = 0 {
        didSet {
            layer.cornerRadius = self.cornerRadius
            layer.masksToBounds = true
        }
    }

    /* ... */
}

This works completely fine within UIViews in my storyboard. However, I want this to also work with UIImageViews and other subclasses of UIView. Is this possible without subclassing my GEView, by somehow making this a generic?

George
  • 25,988
  • 10
  • 79
  • 133

1 Answers1

1

Move your code to UIView's @IBDesignable extension like below:

@IBDesignable extension UIView {    
    @IBInspectable var cornerRadius: CGFloat {
        set {
            layer.cornerRadius = newValue
            layer.masksToBounds = true
        }
        get {
            return self.cornerRadius
        }
    }
}

Clear unwanted values/types of previously deifned vars in your GEView. Here are values in inspector.

enter image description here

Properties available in inspector of any UIView, included itself.

Properties avaliable in storyboard interface

Agisight
  • 1,778
  • 1
  • 14
  • 15
  • wait plz. I try to got it – Agisight Apr 13 '19 at 11:40
  • You can set default on storyboard. no need to set right here. Remove any `@IBInspectable var` in subclasses of `UIView`. Did it helped? – Agisight Apr 13 '19 at 12:09
  • Try to clear some values in User Defined Runtime Attributes on Identify Inspector in Storyboard. Some incorrect values can be stored there – Agisight Apr 13 '19 at 12:11
  • Ok, this is super weird. Now anything but `UIView`s are useable in storyboard – George Apr 13 '19 at 12:12
  • There is some `User Defined Runtime Attributes` for `GEView` which are `UIView`s, but don't show up in the inspector pane. – George Apr 13 '19 at 12:12
  • I use this code in my project and works very well. Seems to be you have some incorrectly overrided values... – Agisight Apr 13 '19 at 12:14
  • I'm not getting any properties to see and change in storyboards for `UIView`s I'm just going to stick with what *@Sulthan* said of wrapping them in `UIView` - however this complicates the hierarchy, so if this can be made to work, it would be better. – George Apr 13 '19 at 12:18
  • Oh wait no... my stupid self of not realising it says `newValue` in your code... – George Apr 13 '19 at 12:20
  • I only get the default properties such as `Content Mode` and such, and I have made sure that the class is set to `GEView`. – George Apr 13 '19 at 12:29
  • How/Where you want to get the properties? – Agisight Apr 13 '19 at 12:29
  • I have properties available in `UIView` subclasses, yay! However, I'm no longer getting them for `UIView` itself. – George Apr 13 '19 at 12:32
  • I don't understand you. All you need are reachable. I updated an answer – Agisight Apr 13 '19 at 12:36
  • Once again, my dumb self... I still had my `UIView` as a `GEView` - I should have removed it – George Apr 13 '19 at 12:40
  • Also, just quickly, how do I stop this function calling itself? (https://imgur.com/dlzYZhU) Thanks! – George Apr 13 '19 at 12:53
  • Change set to willSet, remove get {}. If you create new question – I could answer with code) – Agisight Apr 13 '19 at 14:13
  • I am not using this `extension` method as I want shadows, which are not possible with `extensions`. I will use my class as before where all views to be rounded were encapsulated by a `UIView` set to a `GEView`. However, as this did answer my initial question, I will still accept it as the answer. – George Apr 13 '19 at 19:39