CosmicMind/Samples

In CheckButton Unable to change the background color

MuhammadNayabButt opened this issue · 11 comments

I have configured CheckButton in my project. But I am unable to change its background color. I have gone through the code but I found no way to change it
screen shot 2018-08-29 at 4 26 30 pm
s

Hey! Did you try checkButton.layer.backgroundColor = UIColor.red.cgColor?

@OrkhanAlikhanov will try tomorrow and let you know.

Thank you! No rush :)

No success.
Here is the checked and unchecked button screenshots
checked
unchecked

With little deep digging I came to know that its in "CheckBoxLayer" class which is inside CheckButton.swift
Here we have a variable "selectedColor" and "normalColor" I find no way of changing this "selectedC olor" attribute. This is my investigation and I might be out of track but at-least this is what it appears to me

override func prepareForFirstAnimation() { borderLayer.borderColor = (isEnabled ? (isSelected ? selectedColor : normalColor) disabledColor).cgColor if isSelected { borderLayer.borderWidth = borderLayerNormalBorderWidth } else { borderLayer.backgroundColor = (isEnabled ? normalColor : disabledColor).cgColor checkMarkLeftLayer.strokeEnd = 1 checkMarkRightLayer.strokeEnd = 1 } checkMarkLayer.transform = .identity }

Ah, by backgroundColor you mean selectedColor? You should use setIconColor for the checkbox itself.

let checkButton = CheckButton()
checkButton.title = "Check button"
checkButton.backgroundColor = .gray
checkButton.setIconColor(.green, for: .selected)
checkButton.setIconColor(.blue, for: .normal)
checkButton.checkmarkColor = .yellow

Here is the ugly result:

@OrkhanAlikhanov
Thanks man it is working

@OrkhanAlikhanov I just came across this thread while having an even uglier result:

screen shot 2018-09-03 at 18 01 20

When selected, there is a blue border that wraps around the text. I tried endless combinations how to remove it with no luck. I'm using the same snippet you posted but the CheckBox does this...

So in fact for any future references - when CheckBox is initialized via Storyboard, it's important to set Tint in the inspector to Clear Color. Solves the problem.

@svachmic That is a great point. Thank you.
@OrkhanAlikhanov Let's consider adding this the core configuration of this component.

@svachmic We are using isSelected property of the UIButton when the CheckButton is checked. That blue rounded box around the text is default look of system UIButton when it's selected. You should've changed type of button from System to Custom from Attributes inspector.
image