setTitleColor not working without setTitle - swift

I want to change the tint color of a button.
button.setTitle("✸", for: .normal)
button.setTitleColor(.green, for: .normal)
This is working fine with the code above. However when I remove the first line (setTitle), it does not work anymore.
I have set a title for the button in the interfacebuilder/storyboard. I can't figure out what is the reason for that.
More research. Just one of these options work when the title is set via the interface builder. But when I change the title of the Button, option2 does not longer work.
// option 1
button.setTitle("✸", for: .normal)
button.setTitleColor(.green, for: .normal)
// option 2
button.tintColor = .red

I want to change the tint color of a button.
So why don't you set the tintColor?
button.tintColor = .red
...this works even if you set the title only in the interface builder.

Related

How can I change the placeholder (label) color for MDCOutlinedTextFields?

I'm having a hard time changing the text color for the placeholder label that is used as a hint when not in focus and as a label on top when in focus. (Photo below)
The is reason I'm switching out all my MDC-TextFields and MDC-TextInputControllers is because they are all being deprecated for the New MDC-Outlined Textfields.
The code below is a function within an extension that would simply setup the general background & sub-label colors for all MDC-Outlined Textfields throughout the app.
I have tried a number of functions with no luck(commented out below).
extension MDCOutlinedTextField {
func setUpGeneralBackgroundColors(){
//Text color
self.setTextColor(UIColor.white, for: .normal)
self.setTextColor(UIColor.white, for: .editing)
//Border color
self.setOutlineColor(UIColor.white, for: .normal)
self.setOutlineColor(UIColor.white, for: .editing)
//self.setFloatingLabelColor(UIColor.white, for: .normal)
//self.setFloatingLabelColor(UIColor.white, for: .editing)
//self.setFloatingLabelColor(UIColor.white, for: .disabled)
// self.setNormalLabelColor(UIColor.purple, for: .normal)
// self.setNormalLabelColor(UIColor.purple, for: .editing)
// self.setNormalLabelColor(UIColor.purple, for: .disabled)
// self.label.tintColor = UIColor.purple
//self.label.textColor = UIColor.systemPink
//self.label.shadowColor = UIColor.cyan
//self.label.backgroundColor = UIColor.red
//Changes icon colors within the text field if any
self.tintColor = .green
}
}
I figured the problem, in the storyboard where I have these new textfields I had a raw string for the placeholder which override the swift code behind the scenes and prevented me from changing colors.
Note: This problem will NOT occur with the now deprecated MDC-Textfields.
If you guys have a placeholder value in the storyboard when using these MDC-Outlined Textfields in storyboards, (not to mixed up with the swift code) then get rid of them.

Setting isSelected to true not changing image

I have a tableview cell that has an image for a like button that is set like this,
upArrow.setImage(UIImage(named: "likeButtonFill"), for: .selected)
upArrow.setImage( UIImage(named: "likeButton"), for: .normal)
however, whenever I set
upArrow.isSelected = true
the image on the button never changes.

Button title does not show up

I got a UIButton with a background-image and a title. Wenn it's pressed (and holding), I should change it's background-image (WHICH DOES WORK), but keep it's title. But when I press the button, the title disappears and even when released, it also doesnt shop up again.
#IBAction func holdingTheButton(_ sender: Any) {
numberButton.setImage(UIImage(named: "buttonIn.png"), for: .normal)
numberButton.setTitle("Refuel", for: .normal)
}
This is a part of my holdingTheButton function (already declared/ initialized the button etc.), where I try to keep the title "Refuel". Btw: Changing to "for: .highlighted" or "for: .selected" doesnt change anything.
Screenshot of my Buttons configuration
When you press the UIButton it state is changed to highlighted when it's clicked. UIButton has different states such as selected, normal, highlighted and more.
If you would like the image and the title to stay the same you have got to call again "setTitle" and "setImage" for state "highlighted"
In your "ViewDidLoad" do the following:
numberButton.setImage(UIImage(named: "buttonIn.png"), for: .normal)
numberButton.setTitle("Refuel", for: .normal)
numberButton.setImage(UIImage(named: "buttonIn.png"), for: .highlighted)
numberButton.setTitle("Refuel", for: .highlighted)

Image Not Removing For Disable state of UIButton

I made a button that is able to toggle the own isEnabled, and It's updating the title and image of button according to this state.
myButton.setTitle("Enabled Title", for: .normal)
myButton.setImage(UIImage(named: "enabled_resource_name"), for: .normal)
myButton.setTitle("Disabled Title", for: .disabled)
myButton.setImage(nil, for: .disabled)
The isEnabled of my button has toggled well. and title has also changed according to that.
But I found a strange problem about changing image.
In the enabled to disabled case, the image UIImage(named: "enabled_resource_name") doesn't removed.
But It has changed a little. the image has become a little transparent when It's disabled. and in the disabled to enabled case, It does work fine.
Why does this happens?
Try to set UIImage() instead of nil, like:
myButton.setImage(UIImage(), for: .disabled)

QRCodeReader.swift toggleTorchButton selected image dont work

I'm using QRCodeReader.swift and the possibility to change layout is good, but the selected option of button don't work.
toggleTorchButton?.setImage(UIImage(named: "ic_light_on"), for: .normal)
toggleTorchButton?.setImage(UIImage(named: "ic_light_off"), for: .selected)
If I test the .highlighted state this works fine, but .selected never.
Why?
I'm create a target and this change the selected state.
toggleTorchButton?.addTarget(self, action: #selector(self.toggleTorchButtonHandler), for: .touchUpInside)
func toggleTorchButtonHandler() {
toggleTorchButton?.isSelected = !(toggleTorchButton?.isSelected)!
}