Swift set UIButton setBorderColor in Storyboard

守給你的承諾、 提交于 2019-12-12 16:31:45

问题


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

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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/42041809/swift-set-uibutton-setbordercolor-in-storyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!