Как исправить 'Цвет фона с альфа' - PullRequest
0 голосов
/ 28 мая 2019

У меня такая проблема: если я зажимаю уже выбранную ручку, она становится полностью белой, но мне нужно, чтобы фон стал прозрачным, а цвет надписи остается прежним.

enter image description here

let selectedAttributes = [
        NSAttributedString.Key.foregroundColor: UIColor.red,
        NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 13)
    ]

    let normalAttributes = [
        NSAttributedString.Key.foregroundColor: UIColor.white,
        NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 13
    ]

    self.setTitleTextAttributes(selectedAttributes, for: .selected)
    self.setTitleTextAttributes(normalAttributes, for: .normal)
    self.layer.cornerRadius = 17
self.tintColor = UIColor.white
    self.setBackgroundColor(color: UIColor.white, forState: .selected)
    self.setBackgroundForSelected(color: UIColor.white, for: .selected)
    self.setBackgroundForSelected(color: UIColor.white, for: .highlighted)
    self.setBackgroundColor(color: UIColor.white.withAlphaComponent(0.2), forState: .highlighted)
    self.clipsToBounds = true
    self.layer.borderWidth = 1
    self.layer.borderColor = UIColor.white.cgColor

}

func setBackgroundColor(color: UIColor, forState: UIControl.State) {
    UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
    UIGraphicsGetCurrentContext()?.setFillColor(color.cgColor)
    UIGraphicsGetCurrentContext()?.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
    let colorImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    self.setBackgroundImage(colorImage, for: forState, barMetrics: .default)
}
...