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.
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)
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)!
}
I am creating a custom button which (almost) works fine. It is supposed to change it's image when the button is pressed down, and the change back again when you let loose. The problem is that it takes one click before it starts working.
#IBOutlet weak var myButton: UIButton!
#IBAction func myButton(sender: UIButton) {
myButton.setImage(UIImage(named: "myImage.png")!, forState: .Normal)
myButton.setImage(UIImage(named: "myImagePressed.png")!, forState: .Highlighted)
myButton.setImage(UIImage(named: "myImagePressed.png")!, forState: .Selected)
}
Since you've put your code to the IBAction, it only gets executed once the button is pressed. It is also unecessarily called every time the button is pressed. You need to run that code before the button is pressed. The best place is probably viewDidLoad() of the viewcontroller that controls the view the button is part of.
I'm trying to change the text of a UIButton when it gets clicked. I've tried the following things, which failed:
First I tried inserting a button on the storyboard and linking it to the view controller, with the code for the button appearing like this:
#IBAction func button(sender: AnyObject) {}.
Within the button's parentheses, I then used
button.setTitle ("something" , for State: .Selected)
However, when I run this code in the simulator, it doesn't seem to work at all.
I have also tried, following the Apple reference manual, to set different labels for different states in the side menu after clicking on said button, but still this didn't work.
Can anyone tell me exactly (i.e. what code to write and where exactly it goes) how to make this happen?
The type of the sender should be UIButton, when creating the IBAction function.
The sender is your button. The function is called when you tap on the button. You must use the function setTitle on the sender(your button).
#IBAction func buttonTouchedUpInside(sender: UIButton) {
sender.setTitle("buttonName", forState: .normal)
}
Swift 4:
Plain Title
The setTitle() method will work for titles that are "Plain" as defined in the button's Attributes inspector.
#IBAction func button(sender: UIButton) {
sender.setTitle("buttonName", for: .normal)
}
Attributed Title
The setTitle() method has no effect on a button's title if it's configured as "Attributed" in the Attributes inspector. To manage this situation, first get the attributed title from the button, then set the value.
#IBAction func button(sender: UIButton) {
let attributedTitle = sender.attributedTitle(for: .normal)
attributedTitle?.setValue("buttonName", forKey: "string")
sender.setAttributedTitle(attributedTitle, for: .normal)
}
In practically, if you use button from Storyboard or Xib file, and not clear button value in (Storyboard, Xib), than setTitle method doesn't work. If you cleared button value, setTitle method is work.
Create an IBOutlet for your button, then this code should work (assuming you named your IBOutlet button):
button.setTitle("title", forState: .Normal)
button.setTitle("title 1", forState: .Application)
button.setTitle("title 2", forState: .Highlighted)
button.setTitle("title 3", forState: .Reserved)
button.setTitle("title 4", forState: .Selected)
button.setTitle("title 5", forState: .Disabled)
place it in your viewDidLoad() method.
This worked for me.
dispatch_async(dispatch_get_main_queue(), ^{
[self.btnAssment setTitle:#"ASSDate" forState:UIControlStateNormal];
});
let button = UIButton()
button.setTitle("Button", for: .normal)
button.setTitleColor(.black, for: .normal)
I just had a similar issue, and none of the answer here worked in my case.
For me the solution was to call button.layoutSubviews() to update UIButton's label just after i changed the title.
(also I had to change the button type to custom, otherwise the title change would blink)