UIButton background color for highlighted/selected state issue

前端 未结 6 1845
隐瞒了意图╮
隐瞒了意图╮ 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:31

    Update Swift 4

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

    Event Button Action Show Touch On Highlight

提交回复
热议问题