UIButton background color for highlighted/selected state issue

前端 未结 6 1861
隐瞒了意图╮
隐瞒了意图╮ 2021-02-18 18:47

I\'ve a UIButton and I\'ve created an extension to add background color for different state.

I\'m using the following code:



        
6条回答
  •  伪装坚强ぢ
    2021-02-18 19:53

    Swift 5, IOS 13+

    extension UIButton {
    
      func setBackgroundColor(_ color: UIColor, for 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)
      }
    
    }
    

提交回复
热议问题