Search code examples
swiftxcode10ios12

Preventing button image highlight when selected swift 4


I do my swift project using code only no storyboards, so no storyboard solutions please I have an agree to terms button and when selected the checkmark is covered in the tint color I am trying to prevent the tint color highlighting the image when selected I am not sure why this is happening here is the button code

let agreeToTermsAndConditions : UIButton = {
    let button = UIButton(type: .system)
    button.tintColor = .gray
    button.backgroundColor = .white
    button.adjustsImageWhenHighlighted = false  
    button.setImage(UIImage(named:"unchecked" )!, for: .normal)
    button.setImage(UIImage(named:"agreechecked" )!, for: .selected)
    button.addTarget(self, action: #selector(userAgreedToTerms), for: .touchUpInside)
    
    return button
}()

enter image description here

The code is running on xcode 10.2.1 using swift 4 and iOS 12 deployment


Solution

  • Change

    UIButton(type: .system)
    

    to

    UIButton(type: .custom)
    

    Now you have complete control over the look and behavior of the button.