changing UIButton image after click a button - ios

#IBAction func play(_ sender: UIButton) {
if player.isPlaying == true {
let myImage = UIImage(named:"stop")
sender.setImage(myImage, for: .normal)
if player.isPlaying == false {
(sender as AnyObject).setImage(UIImage(named: "play"), for: .normal)}
}
}
after I click the button, Its button image changes but it never goes back afterwards. how should I make it go back to the original button image?

I partially understand your question, try this
#IBAction func play(_ sender: UIButton) {
sender.setImage(UIImage(named:player.isPlaying ? "stop" : "play") , for: .normal)
}

#IBAction func play(_ sender: UIButton) {
if player.isPlaying == true {
let myImage = UIImage(named:"stop")
sender.setImage(myImage, for: .normal)
} else {
sender.setImage(UIImage(named: "play"), for: .normal)
}
}

If you don't stop the player when you press the button, isPlaying will always be true. So first if conditions works.
#IBAction func play(_ sender: UIButton) {
if player.isPlaying == true {
player.stop()
sender.setImage(UIImage(named:"stop"), for: .normal)
}
if player.isPlaying == false {
player.play()
sender.setImage(UIImage(named: "play"), for: .normal)
}
}

Related

How Can I Function Didtap Button Repeat One Song In Swift?

I am trying to repeat a song in my app. How can I put in a loop feature for this?
#IBAction private func handlePauseTouchUpInside(_ sender: UIButton) {
if player?.timeControlStatus == .paused {
player?.play()
isPause = false
playPauseButton.setImage(UIImage(imageLiteralResourceName: "ic-pause"), for: .normal)
rotateView()
} else {
player?.pause()
isPause = true
playPauseButton.setImage(UIImage(imageLiteralResourceName: "ic-play"), for: .normal)
}
}
//here function repeat
#IBAction private func repeatButtonTapped(_ sender: UIButton) {
enter code here
}
(Xcode Version 13.4.1 (13F100))

How do I save a UIButton being selected (showing checkmark) and load it in iOS?

I am a beginner and the following is what code I have so far. When clicking the button, it'll switch between the checkmark and the empty box (that's good). However, when exiting the app, the button won't save the state of being selected (check marked).
Attached is all my code, so that you can clearly see what's going on.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var checkMarkButton: UIButton!
let save = "save"
override func viewDidLoad() {
super.viewDidLoad()
if UserDefaults.standard.bool(forKey: "save") == true {
checkMarkButton.setImage(#imageLiteral(resourceName: "greyChecked"), for: .normal)
}else{
checkMarkButton.setImage(#imageLiteral(resourceName: "greyUnchecked"), for: .normal)
}
}
#IBAction func checkMarkAction(_ sender: UIButton) {
UserDefaults.standard.set(true, forKey: "save")
}
}
video of what is happening
Give the image name directly ,I tested my answer it is working
override func viewDidLoad() {
super.viewDidLoad()
if UserDefaults.standard.bool(forKey: "checkBoxSave01") == true {
checkBoxButton1.setImage(#imageLiteral(resourceName: "id"), for: .normal)
}else{
checkBoxButton1.setImage(#imageLiteral(resourceName: "phone"), for: .normal)
}
}
var isFirstClick = true
#IBAction func checkBoxTapped(_ sender: UIButton) {
if isFirstClick{
checkBoxButton1.setImage(#imageLiteral(resourceName: "id"), for: .normal)
UserDefaults.standard.set(true, forKey: "checkBoxSave01")
ifFirstClick = false
}else{
checkBoxButton1.setImage(#imageLiteral(resourceName: "phone"), for: .normal)
UserDefaults.standard.set(false, forKey: "checkBoxSave01")
isFirstClick = true
}
}
Once you click on button checkBoxSave01key value will be true until you delete the app.

Toggle between select/deselect UIButton

I have a button which I want to change background color and text when tapped(select) and bring it back to its original state when tapped again (deselect). I am able to select it but I am not being able to deselect it. I have researched on SO and I am getting errors in this code in the if-else part
#IBAction func btn1Pressed(_ sender: AnyObject) {
sender.setTitleColor(UIColor.blue, for: UIControlState.normal)
(sender as! UIButton).backgroundColor = UIColor.green
if sender.isSelected {
sender.selected = false
}
else {
sender.selected = true
}
}
Try this:
#IBAction func btnPressed(_ sender: AnyObject) {
guard let button = sender as? UIButton else { return }
if !button.isSelected {
button.isSelected = true
button.setTitleColor(UIColor.blue, for: UIControl.State.normal)
button.backgroundColor = UIColor.green
}
else {
button.isSelected = false
button.setTitleColor(UIColor.green, for: UIControl.State.normal)
button.backgroundColor = UIColor.blue
}
}
It sounds to me like UISwitch is what you're looking for. Give it a try instead of wasting time implementing something that's already there for you.
try this
#IBAction func btn1Pressed(sender: UIButton) {
if sender.selected {
sender.selected = false
sender.setTitleColor(UIColor.redColor(), forState: .Normal)
sender.backgroundColor = UIColor.blackColor()
}
else {
sender.selected = true
sender.setTitleColor(UIColor.redColor(), forState: .Selected)
sender.backgroundColor = UIColor.blackColor()
}
}
Change background color of selected button from storyboard like below in state config choose selected and then change background color and also coustomize default for normal button.
and use below in action
(sender as! UIButton).selected = !(sender as! UIButton).selected
Please try this code first you need to crate action and propert of the button like
#IBOutlet var btn_ButtonPressed: UIButton!
then Action.
#IBAction func click_ButtonPressed(_ sender: Any) {
if !sender.isSelected() {
sender.selected = true
btn_ButtonPressed.setTitleColor(UIColor.red, forState: .normal)
btn_ButtonPressed.backgroundColor = UIColor.yellow
}
else {
sender.selected = false
btn_ButtonPressed.setTitleColor(UIColor.yellow, forState: .normal)
btn_ButtonPressed.backgroundColor = UIColor.red
}
}
this one worked fine for me!
//
#IBAction func buttonColorChanger(sender : UIButton ) {
if button.isSelected == false
{
button.backgroundColor = UIColor.purple
print("selected")
button.setTitle("selected", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.isSelected = true
}else{
button.backgroundColor = UIColor.white
print("unselected")
button.isSelected = false
}
}
A cleaner approach would be:
#objc private func buttonTapped(_ sender: UIButton) {
sender.isSelected.toggle()
if selected {
// Setup appearance for selected state.
} else {
// Setup appearance for deselected state.
}
}

UIButton text returns to original value after setting a new value to it

My UIButton is in a UICollectionviewcell, which is in a UICollectionview, which is in a UITableViewCell, which is in a UITableView.
The button is a like button, I want it to change its text when clicked. But after changing the text, it returns the original value. What might be the reason here?
Here is what it looks like:
https://gfycat.com/UnlawfulGroundedErin
This is the onClickEvent of button:
#IBAction func actionClick(sender: MyActionButton) {
if (sender.toggle){
print("Do dislike call here.")
sender.toggle = false
sender.titleLabel?.text = "Like"
}else{
print("Do like call here.")
sender.toggle = true
sender.titleLabel?.text = "Unlike"
}
}
Try This. It will work properly
#IBAction func actionClick(sender: MyActionButton) {
if (sender.toggle){
print("Do dislike call here.")
sender.toggle = false
sender.titleLabel?.text = "Like"
sender.setTitle("Like", forState: UIControlState.Normal)
}else{
print("Do like call here.")
sender.toggle = true
sender.setTitle("Unlike", forState: UIControlState.Normal)
}
}
UIButton have different texts for its title in different states.
Just for your knowledge
In viewDidLoad:
btnObject.setTitle("Like", forState: UIControlState.Normal)
btnObject.setTitle("Unlike", forState: UIControlState.Selected)
then in method actionClick: if you write only one statement,
sender.selected = !sender.selected;
it will work.
try this
#IBAction func actionClick(sender: UIButton) {
if sender.isSelected() {
sender.selected = false
sender.setTitle("Like", forState: UIControlState.Normal)
}
else {
sender.selected = true
sender.setTitle("Unlike", forState: UIControlState.Normal)
}
}
Choice-2
var isSelectFirst:Bool = false
#IBAction func actionClick(sender: UIButton) {
if isHighLighted == false{
sender.setTitle("Unlike", forState: UIControlState.Normal)
isHighLighted = true
}else{
sender.setTitle("Like", forState: UIControlState.Normal)
isHighLighted = false
}
}

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
}

Resources