如何在Swift中使用MDCButtons自定义轮廓按钮?

时间:2019-04-09 03:24:01

标签: swift material-design

我正在使用UIButton自定义MDCButton。我想使按钮轮廓化并自定义其颜色。

在这种情况下,我正在使用MDCOutlinedButtonThemer。 我还使用其他按钮中的自定义颜色实现了MDCButtonMDCButtonColorThemer)并可以正常工作。 我尝试将按钮设置为默认MDCOutlinedButton,它可以正常工作。

这是我的代码:

MDCOutlinedButtonThemer.applyScheme(buttonScheme, to: self.btnAddToCart)

MDCButtonColorThemer.applySemanticColorScheme(ApplicationScheme.shared.colorScheme, to: self.btnBuy)

这是ApplicationScheme.swift:

public let colorScheme: MDCColorScheming = { 
    let scheme = MDCSemanticColorScheme(defaults: .material201804)
    //TODO: Customize our app Colors after this line
    scheme.primaryColor = UIColor(red: 255.0 / 255.0, green: 201.0 / 255.0, blue: 46.0 / 255.0, alpha: 1)
    //scheme.primaryColorVariant = UIColor(red: 68.0/255.0, green: 44.0/255.0, blue: 46.0/255.0, alpha: 1.0)
    //scheme.onPrimaryColor = UIColor(red: 255.0/255.0, green: 201.0/255.0, blue: 46.0/255.0, alpha: 1.0)
    scheme.secondaryColor = UIColor(red: 254.0/255.0, green: 201.0/255.0, blue: 46.0/255.0, alpha: 1.0)
    //scheme.onSecondaryColor = UIColor(red: 68.0/255.0, green: 44.0/255.0, blue: 46.0/255.0, alpha: 1.0)
    scheme.surfaceColor = UIColor(red: 255.0/255.0, green: 201.0/255.0, blue: 46.0/255.0, alpha: 1.0)
    //scheme.onSurfaceColor = UIColor(red: 255.0/255.0, green: 201.0/255.0, blue: 46.0/255.0, alpha: 1.0)
    scheme.backgroundColor = UIColor(red: 255.0/255.0, green: 201.0/255.0, blue: 46.0/255.0, alpha: 1.0)
    //scheme.onBackgroundColor = UIColor(red: 68.0/255.0, green: 44.0/255.0, blue: 46.0/255.0, alpha: 1.0)
    //scheme.errorColor = UIColor(red: 197.0/255.0, green: 3.0/255.0, blue: 43.0/255.0, alpha: 1.0)
    return scheme
}()

enter image description here

我要使“添加到购物车”按钮的边框颜色与“购买”按钮的颜色相同

1 个答案:

答案 0 :(得分:0)

您可以使用下一个颜色设置来创建MDCButton:

let myButton = MDCButton()

myButton.setTitle("Add to cart", for: .normal)
myButton.isUppercaseTitle = false
myButton.addTarget(self, action: #selector(myButtonAction(_:)), for: .touchUpInside)
myButton.frame = CGRect(x: 10, y: 10, width: 150, height: 40)
myButton.setBackgroundColor(UIColor.white)
myButton.setTitleColor(UIColor.blue, for: UIControl.State.normal)
myButton.setBorderColor(UIColor.lightGray, for: UIControl.State.normal)
myButton.setBorderWidth(1.0, for: UIControl.State.normal)
myButton.layer.cornerRadius = 5

view.addSubview(myButton)