How to change UIButton image in Swift
From your Obc-C code I think you want to set an Image for button so try this way:
let playButton = UIButton(type: .Custom)
if let image = UIImage(named: "play.png") {
playButton.setImage(image, forState: .Normal)
}
In Short:
playButton.setImage(UIImage(named: "play.png"), forState: UIControlState.Normal)
For Swift 3:
let playButton = UIButton(type: .custom)
playButton.setImage(UIImage(named: "play.png"), for: .normal)
How to change UIButton's image every time it is pressed
Thanks for mentions of setBackgroundImage
to Xavier L. from the other answer.
Try the following code:
import UIKit
class ViewController: UIViewController {
@IBOutlet var button: UIButton!
var buttonActive = false
override func viewDidLoad() {
super.viewDidLoad()
button.setBackgroundImage(UIImage(named: "apple"), for: .normal)
}
@IBAction func buttonPressed() {
if buttonActive {
button.setBackgroundImage(UIImage(named: "apple"), for: .normal)
} else {
button.setBackgroundImage(UIImage(named: "pineapple"), for: .normal)
}
buttonActive = !buttonActive
}
}
And your storyboard should look like that:
N.B. On Stack Overflow you should perform some actions before asking. Try to include some code you tried.
i want to change UIButton and UIImage view by clicking on a button. But images for UIButton and Image view are different
I hope I got your question correct. You have a button with an image on it and an imageview. 2 separate things. And you want to change your images.
If you want to change your buttons image by clicking on it you should not only have IBAction but also IBOutlet for that button. Control drag and make an outlet for it. In the button's IBAction function just change your button's image to what you want, as well as your image view's.
UIButton image won't change when I click on it
Try using this method to set the button's image programatically. Set two different images for the states normal
and selected
.
checkMarkButton.setImage(UIImage(named: "selected_image"), for: .selected)
checkMarkButton.setImage(UIImage(named: "normal_image"), for: .normal)
Then in your selector
:
@objc func handleTap(sender: UIButton) {
sender.isSelected = !sender.isSelected
}
Changing image of UIButton via click - Swift
You only needs to change (sender:AnyObject) to (sender:UIButton)
as below
@IBAction func tick(sender: UIButton) {
if let image = UIImage(named:"Unchecked") {
sender.setImage(UIImage(named:"Checked.png"), forControlState: .Normal)
}
if let image = UIImage(named:"Checked") {
sender.setImage( UIImage(named:"Unchecked.png"), forControlState: .Normal)
}
}
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"))
}
Swift: How to Change UIButton Image on clicking submit button
@IBAction func submitActn(sender : UIButton!){
checkBoxBtn.imageView?.image = UIImage(named: "image") //Similar for every checkBtn
textField1.text = ""
}
Or a much elegant way:-
Go to your Storyboard -> Select your buttons one at a time -> open Their Attributes Inspector ->
For State Config -Default, select a image in image
box just below that you want to show as default(uncheck image )
For State Config -Selcted, select a image in image
box just below that you want to show as when the button is selected(checked image)
@IBAction func submitActn(sender : UIButton!){
checkBtn1.selected = false
checkBtn2.selected = false
checkBtn3.selected = false
checkBtn4.selected = fasle
textField1.text = ""
}
Related Topics
Autolayout: Uiview Within Uiview Has Incorrect Frame
Disable Autorotate on a Single Subview in iOS8
How to Differentiate Whether a User Is Tapping the Screen with His Finger or an Apple Pencil
Having Uiview Drawrect Occur in a Background Thread
Custom Uitableviewcell with Progress Bar Download Update
iOS Appextension:How to Combine Nsextensionactivationrule and Nspredicate
Detecting iOS Version Number from User Agent Using Regular Expressions
How to Send a Uilongpressgesture Programmatically
Fit Image of Random Size into a Uiwebview (Ios)
Ios: Diddiscoverperipheral Not Called in Background Mode
How to Change Uibutton Image After Clicking in Swift
Map Url Parameters to Objects Using Restkit
Why Is -Force_Load No Longer Required for My Three20 Dependencies in Xcode 4.2
Making Uitableview with Embedded Uicollectionview Using Uitableviewautomaticdimension