How to change its own button image on tap in Swift
Do it like this
@IBAction func btnSmiley(sender: UIButton) {
if let image = UIImage(named:"smiley") {
sender.setImage(image, forControlState: .Normal)
}
}
How do I change the image of a button when tapped - Swift
Though Dharmesh Kheni's answer is OK I would recommend to use two buttons. You can set them in separate function or in interface builder and then just show and hide them according to state. Something like this:
func setButtons()
{
playBtn.setImage(UIImage(named: "play.png"), forState:.Normal)
pauseBtn.setImage(UIImage(named: "play.png"), forState:.Normal)
pauseBtn.hidden = true
}
@IBAction func playTapped(sender: AnyObject)
{
playBtn.hidden = true
pauseBtn.hidden = flase
//do other stuff
}
@IBAction func pauseTapped(sender: AnyObject)
{
playBtn.hidden = false
pauseBtn.hidden = true
//do other stuff
}
Change the UIButton Image when tapped multiple times
Answer after question edit
A couple tips to make it easier to debug...
Add comments
Use variable names that make sense - e.g.
imgRepeat
instead ofimage2
for an image named "repeat"
The following code works fine for me - assuming the images have transparent areas to show the background color.
@IBAction func repeateButtonPressed(_ sender: Any) {
let imgRepeatOne = UIImage(named : "repeat_one")
let imgRepeat = UIImage(named : "repeat")
if (playerRepeatMode == RepeatModes.NO_REPEAT) {
// currently NO_REPEAT, changing to REPEAT_LIST
playerRepeatMode = RepeatModes.REPEAT_LIST
// set BackroundImage to "repeat"
self.repeatButton.setBackgroundImage(imgRepeat, for: .normal)
// set BackgroundColor to buttonToggleColor
self.repeatButton.backgroundColor = buttonToggleColor
} else if (playerRepeatMode == RepeatModes.REPEAT_LIST) {
// currently REPEAT_LIST, changing to REPEAT
playerRepeatMode = RepeatModes.REPEAT
// set BackroundImage to "repeat_one"
self.repeatButton.setBackgroundImage(imgRepeatOne, for: .normal)
// set BackgroundColor to buttonToggleColor
self.repeatButton.backgroundColor = buttonToggleColor
} else {
// currently REPEAT, changing to NO_REPEAT
playerRepeatMode = RepeatModes.NO_REPEAT
// set BackroundImage to "repeat"
self.repeatButton.setBackgroundImage(imgRepeat, for: .normal)
// set BackgroundColor to clear
self.repeatButton.backgroundColor = UIColor.clear
}
}
Original Answer
I think the issue is that you are using .setImage()
in one place, but .setBackgroundImage()
in the other two places.
That would be fine, assuming you want image2
to be used as background and image
to be used as the button image, but you need to clear the other property.
So, where you use the background image:
self.repeatButton.setBackgroundImage(image2, for: .normal)
// also *clear* the button image
self.repeatButton.setImage(nil, for: .normal)
and, where you use the button image:
self.repeatButton.setImage(image, for: .normal)
// also *clear* the background image
self.repeatButton.setBackgroundImage(nil, for: .normal)
How to change Custom Button's Image when Tapping in iOS?
assuming this is a standard UIButton, you can do the following when initializing the button:
[btnObj setImage:[UIImage imageNamed:@"imgName.png"] forState:UIControlStateHighlighted];
or for the Background image:
[btnObj setBackgroundImage:[UIImage imageNamed:@"imgName.png"] forState:UIControlStateHighlighted];
Change a UIButton's image that was created programmatically within a for loop Swift
This is suspect:
@objc func tap(sender: UIButton) {
DispatchQueue.main.asnyc{
buttonArray[sender.tag].setImage(UIImage(named:"Somename"))
}
You are already on the main queue, and the sender is the button that was tapped. So you don't need the array.
@objc func tap(sender: UIButton) {
sender.setImage(UIImage(named:"Somename"))
}
How to change button image on tapped in repeating cell?
Try:
- Open the Identity Inspector.
- Click the prototype cell.
- Set the
Class
toTasksCell
(in my example, it isWorkTableViewCell
). - Click in the
Module
field, empty it, and press enter. It should change toCurrent - project_name_here
(for example,Current - Artistry
.
Here's a screenshot of what it should look like. Check it to make sure you selected the right items in the right order:
Then, delete the old outlet(s) to the cell (button, label, everything) from the Connections Inspector. Do that step while selecting the cell, and then while selecting the view controller (but while selecting the view controller, only delete the outlet to the "completed" button).
And finally, re-add them by opening the code (in a new window) for your TasksCell
class, not the TaskViewController
class (probably in TasksCell.swift
), and directly under the line:
class TasksCell: UITableViewCell {
(or similar), hold control and drag the button, label, etc. as shown:
And finally, finish creating the new outlets only from the label, button, etc. to the TasksCell class as mentioned earlier (see note below before pressing "connect"):
Change the Name
as necessary. It could be something like taskName
for the label, or completedButton
for the button. Do not change the Type
or any other fields. Press "connect", and the code from your question should work.
Changing a UIButton's image when it's tapped
Set the image for both the UIControlStateHighlighted
and UIControlStateSelected
states:
[addFriendButton setImage:addFriendButtonImage forState:UIControlStateNormal];
[addFriendButton setImage:addFriendButtonImageHighlighted forState:UIControlStateHighlighted];
[addFriendButton setImage:addFriendButtonImageHighlighted forState:UIControlStateSelected];
Then listen for UIControlEventTouchUpInside
:
[addFriendButton addTarget:self action:@selector(handleTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
Then update the selected state:
- (void)handleTouchUpInside:(UIButton *)sender {
sender.selected = !sender.selected;
}
Related Topics
Set Custom Uiview Frame in Uiviewrepresentable Swiftui
Swift Use Unicode Character in Localization.Strings
How to Open Your App's Settings (Inside the Settings App) with Swift (iOS 11)
Why Are Properties of an Immutable Object Mutable in Swift
Phase 1 and Phase 2 Initialization in Swift
Class with Non-Optional Property Conforming to Protocol with Optional Property
Textfield in Swiftui Loses Focus When I Enter a Character
How to Use Nsvisualeffectview to Blend Window with Background
How to Get Mouse Location with Swiftui
How to Define Static Constant in a Generic Class in Swift
How to Unwrap Arbitrarily Deeply Nested Optionals in Swift
Modifying an Array Passed as an Argument to a Function in Swift
Swift - Associated Types in Protocol with Where Clause
Xcodebuild -Exportarchive Wont Allow Me to Specify Filename
Draw a Straight Line in Swift 3 and Core Graphics