In CheckButton Unable to change the background color
MuhammadNayabButt opened this issue · 11 comments
Hey! Did you try checkButton.layer.backgroundColor = UIColor.red.cgColor
?
@OrkhanAlikhanov will try tomorrow and let you know.
Thank you! No rush :)
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
@OrkhanAlikhanov
Thanks man it is working
@OrkhanAlikhanov I just came across this thread while having an even uglier result:
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.