David
David

Reputation: 825

Swift set UIButton setBorderColor in Storyboard

I'm trying to set UIButton Border color on Storyboard:

Set button color

But the button is still Black. When I try to use layer.borderColor, it doesn't show the border at all.

How to se the color?

Thank you

// UPDATE

I've changed the way to set up based on @dfd to:

import Foundation
import UIKit

@IBDesignable

public class OnboardingButton: UIButton {
    @IBInspectable public var borderColor:UIColor? {
        didSet {
            layer.borderColor = borderColor?.cgColor
        }
   }
   @IBInspectable public var borderWidth:CGFloat = 0 {
        didSet {
            layer.borderWidth = borderWidth
        }
   }
   @IBInspectable public var cornerRadius:CGFloat {
        get {
            return layer.cornerRadius
        }
        set {
            layer.cornerRadius = newValue
            layer.masksToBounds = newValue > 0
        }
   }
}

After set up in Storyboard Custom Class to *OnboardingButton app started build but Faield. There is no error in the Log. Where can I find the error and how to fix it?

Upvotes: 3

Views: 3311

Answers (2)

user7014451
user7014451

Reputation:

Here's my UIButton subclass:

@IBDesignable
public class Button: UIButton {
    @IBInspectable public var borderColor:UIColor? {
        didSet {
            layer.borderColor = borderColor?.cgColor
        }
    }
    @IBInspectable public var borderWidth:CGFloat = 0 {
        didSet {
        layer.borderWidth = borderWidth
        }
    }
    @IBInspectable public var cornerRadius:CGFloat {
        get {
            return layer.cornerRadius
        }
        set {
            layer.cornerRadius = newValue
            layer.masksToBounds = newValue > 0
        }
    }
}

EDIT:

I created a simple project and added the above IBDesignable subclass. Below are three screen shots of the result. Note that in the Identity inspector I've set the Class to be Button, not UIButton and that it reports that the Designables are "Up to date". Note that in the Attributes inspector these appear as properties at the very top.

Identity inspector Attributes inspector Storyboard snapshot

Upvotes: 6

K. Michał
K. Michał

Reputation: 1053

You can change some properties of UIButton form interface builder but not color of border. Another way would be subclassing it like in answer suggested by dfd.

Upvotes: 0

Related Questions