答案 0 :(得分:1)
答案 1 :(得分:0)
用户点按时更改背景和文字颜色。
答案 2 :(得分:0)
您可以创建一个UIButton
子类并为不同的控件状态设置背景图像:
class CustomButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
sharedInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
sharedInit()
}
private func sharedInit() {
setBackgroundImage(UIColor.darkGrayColor().image, forState: .Normal)
setBackgroundImage(UIColor.blackColor().image, forState: .Highlighted)
setTitleColor(UIColor.whiteColor(), forState: .Normal)
}
}
extension UIColor {
var image: UIImage {
return imageWithSize(CGSize(width: 1, height: 1))
}
func imageWithSize(size: CGSize) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, true, 0)
self.setFill()
UIRectFill(CGRect(origin: .zero, size: size))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}