UIButton set title image tint color for normal and selected not working

 Problem:

I had a button with image and title with one color for normal state and other color for selected state.

But unfortunately it was not working as expected 


Solution:

extension UIButton {

        func setBackgroundColor(color: UIColor, forState: UIControlState) {

            UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))

            CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), color.CGColor)

            CGContextFillRect(UIGraphicsGetCurrentContext(), CGRect(x: 0, y: 0, width: 1, height: 1))

            let colorImg = UIGraphicsGetImageFromCurrentImageContext()

            UIGraphicsEndImageContext()

            self.setBackgroundImage(colorImg, forState: forState)

        }

    }


If the above code not works then check with your asset image.


If the above option if render as option selected as template image then it will not works.If
It will be selected as default then only it will works


Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Saved Image in document directory and save path in coredata not able to fetch the image file iOS swift