Hide Title of Button in ios Swift and show image instead - ios

I want to hide the Label text of a button onClick and instead show an image.
On a second click, the title should appear again.
Sadly, the title disappears and the color of the button changes but there is no image shown and on click it never enters the "else"-Part of the if/else where the label should appear back again, so "2" is never printed.
What is the error?
if (button10.titleLabel!.text != "") {
print("1")
button10.setTitle("", forState: .Normal)
button10.setImage(UIImage(named: "1.png"), forState: UIControlState.Normal)
}
else if (button10.titleLabel!.text == ""){
print("2")
button10.setTitle("String", forState: .Normal)
}

You need to remove/change the image for the button too:
#IBOutlet weak var button: UIButton!
var clicked = false
#IBAction func buttonClicked(sender: AnyObject) {
if (clicked){
clicked = false
button.setTitle("", forState: .Normal)
button.setBackgroundImage(UIImage(named: "black"), forState: UIControlState.Normal)
}
else{
clicked = true
button.setBackgroundImage(UIImage(named: ""), forState: UIControlState.Normal)
button.setTitle("Clicked", forState: .Normal)
}
}
And to change the image for a UIButton use setBackgroundImage

Related

How to toggle button text and colour in Xcode 10 - UIControl.State.normal missing

I'm new to swift and watching a tutorial on how to toggle the background colour and text of a button when it is clicked.
The problem is, the background colour and text do not change when the button is clicked.
The function, which is identical to the instructor's and works on his laptop is :
func flipCard(withEmoji emoji: String, on button: UIButton){
if button.currentTitle == emoji{
button.setTitle("", for UIControl.State.normal)
button.backgroundColor = UIColor.red
}else{
button.setTitle(emoji, for UIControl.State.normal)
button.backgroundColor = UIColor.white
}
}
I call the function with:
#IBAction func callFlip(_ sender:UIButton){
flipcard(withEmoji: "em", on: sender)
}
I have searched for a solution but they all seem to refer to older versions of Xcode and swift.
Nevertheless i have tried to to replace
button.setTitle(emoji, for: UIControl.State.normal)
with
button.setTitle(emoji, for: UIControl.State(rawValue:UInt(0))) and
button.setTitle(emoji, for: []) as suggested here with the same result :( . How can i get this to work please?
Im using Xcode 10.1 (10B61). Does the version of Xcode matter?
cheers
Tested this, with a button with title FlipColor and get the inverting effect.
func flipCard(withEmoji emoji: String, on button: UIButton){
if button.currentTitle == emoji {
button.setTitle("", for: UIControl.State.normal)
button.backgroundColor = UIColor.red
button.tintColor = .yellow
}else{
button.setTitle(emoji, for: UIControl.State.normal)
button.backgroundColor = UIColor.white
button.tintColor = .red
}
}
#IBAction func flipMyColor(_ sender: UIButton) {
flipCard(withEmoji : "FlipColor", on: sender)
}

UIButton not changing image

I'm trying to create a "like" button for my app. When I press the button, I want it to change to a second image, and when I press it again I went it to change back to the first image.
Here is my code:
#IBAction func sendLike(sender: UIButton) {
if let _ = UIImage(named:"pinkLike.png") {
sender.setImage(UIImage(named:"like.png"), forState: .Normal)
}
if let _ = UIImage(named:"like.png") {
sender.setImage(UIImage(named:"pinkLike.png"), forState: .Normal)
}
}
For some reason this line of code works how I want it to:
if let _ = UIImage(named:"like.png") {
sender.setImage(UIImage(named:"pinkLike.png"), forState: .Normal)
}
But when I press the button again, this line doesn't change it back:
if let _ = UIImage(named:"pinkLike.png") {
sender.setImage(UIImage(named:"like.png"), forState: .Normal)
}
You set both of those image in Normal state, so your "like.png" will always override "pinkLike.png" image.
You need to put "pinkLike.png" in different state.
if let _ = UIImage(named:"pinkLike.png") {
sender.setImage(UIImage(named:"like.png"), forState: .Selected)
}
and change your selected state before setting the image. So your code will be like this.
#IBAction func sendLike(sender: UIButton) {
sender.selected = !sender.selected
if let _ = UIImage(named:"pinkLike.png") {
sender.setImage(UIImage(named:"like.png"), forState: .Normal)
}
if let _ = UIImage(named:"like.png") {
sender.setImage(UIImage(named:"pinkLike.png"), forState: .Selected)
}
}

Select/deselect buttons swift xcode 7

Part way done with learning swift but I hit a small wall and yet again, I'm sure I'm just a bit new at this and an easy solution is there but I'm having trouble figuring out how to select/deselect buttons below is what I have so far and it is a button turns into a checkmark when clicked on... I've gotten that far but I need that button to deselect when clicked on again and then obviously be able to be clicked again if need be.
#IBAction func buttonPressed(sender: AnyObject) {
sender.setImage(UIImage(named: "Checkmark.png"), forState: .Normal)
}
Swift 3 note: .selected and .checked are now lower case UIControlState values in the SDK, and some of the methods have been renamed:
let button = UIButton()
button.setImage(UIImage(named: "Unchecked"), for: .normal)
button.setImage(UIImage(named: "Checked"), for: .selected)
You can also now use image literals with Xcode 8 instead of UIImage(named:):
#imageLiteral(resourceName: "Unchecked")
Swift 2:
Why not use the .Selected state of the button as the "checked" state, and the .Normal state as the "unchecked" state.
let button = UIButton()
button.setImage(UIImage(named: "Unchecked"), forState: .Normal)
button.setImage(UIImage(named: "Checked"), forState: .Selected)
// ...
#IBAction func buttonPressed(sender: AnyObject) {
if let button = sender as? UIButton {
if button.selected {
// set deselected
button.selected = false
} else {
// set selected
button.selected = true
}
}
}
You dont need to set selected in condition. I just doing with following method in swift:
func selectDeselect(sender: UIButton){
sender.selected = !sender.selected
if(sender.selected == true)
{
sender.setImage(UIImage(named:"select_heart"), forState: UIControlState.Normal)
}
else
{
sender.setImage(UIImage(named:"heart"), forState: UIControlState.Normal)
}
}
Here is Working code for swift 4.
Make Sure you need to connect Button IBAction Outlet as UIButton and set default button image from storyboard whatever you want.
#IBAction func btnTapped(_ sender: UIButton) {
if sender.currentImage == UIImage(named: "radio_unchecked"){
sender.setImage(UIImage(named: "radio_checked"), for: .normal)
}else{
sender.setImage(UIImage(named: "radio_unchecked"), for: .normal)
}
}
update for Swift 4+ :
let button = UIButton()
button.setImage(UIImage(named: "Unchecked"), forState: .Normal)
button.setImage(UIImage(named: "Checked"), forState: .Selected)
#IBAction func buttonPressed(sender: AnyObject) {
if let button = sender {
if button.isSelected {
// set deselected
button.isSelected = false
} else {
// set selected
button.isSelected = true
}
}
}
Set uncheck image on default state from storyboard and check image on selected state from storyboard.
#IBAction func buttonPressed(sender: AnyObject) {
buttonOutlet.isSelected = !buttonOutlet.isSelected
}
private(set) lazy var myButton: UIButton = {
let button = UIButton()
button.setImage(UIImage(named: "Unchecked"), for: .normal)
button.setImage(UIImage(named: "Checked"), for: .selected)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
#objc
func buttonTapped(sender: AnyObject) {
sender.isSelected.toggle()
}
To select only the pressed button out of three buttons and deselect the others when designated button is pressed.
#IBAction func buttonPressed(_ sender: UIButton) {
// Deselect all buttons
button1.isSelected = false
button2.isSelected = false
button3.isSelected = false
// Select the pressed button
sender.isSelected = true
}

Changing image of UIButton when touched

I'm changing the image of my button by using a boolean like this, but I have a couple of problems:
#IBOutlet weak var checkedButtonOutl: UIButton!
var hasBeenTouched = false
override func viewDidLoad() {
super.viewDidLoad()
checkedButtonOutl.setImage(uncheckedImage, forState: UIControlState.Normal)
checkedButtonOutl.addTarget(self, action: "click", forControlEvents: UIControlEvents.TouchUpInside)
checkedButtonOutl.showsTouchWhenHighlighted = false
}
func click(){
if hasBeenTouched == false{
checkedButtonOutl.setImage(uncheckedImage, forState: UIControlState.Normal)
hasBeenTouched = true
}else{
checkedButtonOutl.setImage(checkedImage, forState: UIControlState.Normal)
hasBeenTouched = false
Right after the view has loaded, you have to touch the button two times before it changes. Also, when the button is touched it highlights, which I don't want and I thought the showsTouchWhenHighlighted function would take care of.
Any suggestions on how to fix these two problems would be appreciated.

Swift if statement being re-executed when button title updated

So I have this very simple code, the button is set to the title "Title1" in IB
#IBAction func changeTitle(sender: AnyObject) {
let button = sender as UIButton
if (button.titleLabel == "Title1") {
button.setTitle("Title2", forState: .Normal)
} else {
button.setTitle("Title1", forState: .Normal)
}
}
So what's happening is that the first condition in the if statement gets executed, then the line of code updating the title of the button is hit, where then it goes back to the if statement, then finding it false, and skipping to the else {} now... I cannot figure out why this is happening.
Thanks in advance
This should work:
#IBAction func changeTitle(sender: AnyObject) {
let button = sender as UIButton
if (button.titleLabel?.text == "Title1") {
button.setTitle("Title2", forState: .Normal)
} else {
button.setTitle("Title1", forState: .Normal)
}
}

Resources