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)
Swift set button image
From Apple documentation:
A control becomes highlighted when a touch event enters the control’s
bounds...
Highlighted state of a control
button.setImage(UIImage(named: "button_normal_state"), for: .normal)
button.setImage(UIImage(named: "button_pressed_state"), for: .highlighted)
How to change button image in swift?
You don't need ".png".
If ".imageWithRenderingMode(.AlwaysOriginal)" is working for you: To keep the same image in different states, you have to set the same image/properties for the different states.
@IBOutlet var tapButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
tapButton.setImage(UIImage(named: "redTap")?.imageWithRenderingMode(.AlwaysOriginal), forState: .Normal)
tapButton.setImage(UIImage(named: "redTap")?.imageWithRenderingMode(.AlwaysOriginal), forState: .Highlighted)
}
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.
Xcode: change UIButton image positioning
In storyboard change the title as 'attributed type. Then you can change the font, color, image, its alignment and event the gap between title and image.
'
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.
Can't change UIbutton image for different state
This helped me (swift 3.0)
btn.setImage(UIImage(named:"yourFriend")?.withRenderingMode(.alwaysOriginal), for: .normal)
btn.setImage(UIImage(named:"yourFriend")?.withRenderingMode(.alwaysOriginal), for: .disabled)
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"))
}
Related Topics
App Installation Failed Due to Application-Identifier Entitlement
Ios: Uibutton Resize According to Text Length
Ios4: How to Use Video File as an Opengl Texture
Distancefromlocation - Calculate Distance Between Two Points
Disabling Automatic Scrolling of Uitableview When Editing Uitextfield Inside Uitableviewcell
How to Set Uibutton Background Color Forstate: Uicontrolstate.Highlighted in Swift
Using Ldap for Authentication in iOS
Instantiate View Controller from Storyboard VS. Creating New Instance
How to Show Alertview with Activity Indicator
Nscache Doesn't Work with All Images When Loading for the First Time
Reasons for Rejecting iPhone Application by Apple Store
How to Allow User to Pick the Image with Swift
Uitableviewcell Buttons with Action
How to Turn the iPhone Camera Flash On/Off
Hiding the Tabbar and Removing the Space
Send Programmatically Sms on Jailbreak Device
How to Resize an Image or Done as a Nsattributedstring Nstextattachment (Or Set Its Initital Size)