john doe
john doe

Reputation: 9690

@IBDesignable UIButton Extension

I am trying to extend the UIButton class by adding a cornerRadius property which can be changed at the design time without having to build the app. I am using the following extension class:

import UIKit

@IBDesignable
extension UIButton {

    @IBInspectable var cornerRadius :CGFloat {

        get {
            return layer.cornerRadius
        }

        set {

            layer.cornerRadius = newValue
            layer.masksToBounds = newValue > 0
        }
    }

}

But when I make a change of the property cornerRadius in the Storyboard I do not see the change happening live! Am I missing something!

Upvotes: 10

Views: 3288

Answers (2)

Gaurav Patel
Gaurav Patel

Reputation: 957

try this code:

@IBDesignable extension UIView {
     @IBInspectable var borderColor:UIColor? {
            set {
                layer.borderColor = newValue!.CGColor
            }
            get {
                if let color = layer.borderColor {
                    return UIColor(CGColor:color)
                }
                else {
                    return nil
                }
            }
        }
    }

this will show effect on runtime

Upvotes: -7

Duncan C
Duncan C

Reputation: 131501

Extensions don't honor the IBDesignable qualifier. Only actual subclasses do. Annoying but true.

Upvotes: 18

Related Questions