changing an button image of a tableview - swift

I have been trying to solve a problem, but cannot seem to find a solution.
I have a tableviewcell which contains a button. the button has an image set to it. I would like to change the image when the button is pressed, but I am lacking the proper code for this and where the code needs to be inserted (either in the code inside of tableviewcontoller or tableviewcell file).
I know that the normal code is: self.practicePlayButton.setImage(UIImage(named: "play4.png"), forState: .Highlighted)
for changing the buttons... But I seem to be stumped here.

self.practicePlayButton.setImage(UIImage(named: "play4.png"), forState: .Highlighted)
Replace this...
self.practicePlayButton.setImage(UIImage(named: "play4.png"), forState: .Selected)

Related

iOS custom button can't detect touch up on time

I am new to iOS dev, in android for one minute I make custom button with two images normal and hover. I don't know in iOS swift how to do that, few days I am trying, I try but I can't catch touch so I can switch images for non click and click state. If I do that with button there are some delays for touch up and it's not good. I also try with imageView but it's doesn't work. Any help with this?
Just set different images for the button states (for highlighted state and normal state)
button.setImage(UIImage(named: "normal-img"), for: .normal)
button.setImage(UIImage(named: "dark-img"), for: .highlighted)

Change color of disabled toolbar button

I need to change the color of a disabled toolbar button. I have tried multiple methods.
1.
button.isEnabled = false
button.tintColor = UIColor.blue
2.
button.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blue], for: .disabled)
Neither of them seems to work. Is there a way to change the color of a disabled toolbar item?
note: this item was created in the storyboard and I am loading it from an outlet
edit: I should have explained this earlier but the buttons have pictures and the tint color seems to be the only thing that works for changing the color of the image.
It's Xcode bug. Use UIButton instead. You can just drag and drop it into the tabbar in storyboard.
And then just use title color:
button.setTitleColor(.blue, for: .disabled)
Swift 5
I didn't want my image to be lighter so what worked for me was to force to use that same image for disabled status, like this:
button.setBackgroundImage(UIImage(named: "MyBackgroundColor"), for: .disabled)

Button Highlight in Xcode

I'm trying to display a second button image when the button is tapped. The second image is displaying but the button gets highlighted and it doesn't look very good. How do I get it to show the second image without highlighting it?
button.setBackgroundImage(IMAGE, forState: UIControlState.Normal)

Change UINavigationBar button image

I am trying to replace the image displayed in my right navigation bar button when it is tapped.
At the moment what seems to be happening is that it is not replacing the button, but trying to set it while the old one still exists. This leads to a strange effect as you can see:
It's a compass image, so there should only be one circle, but as you can see three are appearing.
This is my code:
let backImg: UIImage = UIImage(named: "Compass.png")!
navButton.setBackgroundImage(backImg, forState: .Normal, barMetrics: .Default)
Is there a way to remove the existing one and replace it with the new one?
Thanks.
It sounds like perhaps you are setting the UIBarButtonItem's backgroundImage when what you really want to set is its image. Thus you end up seeing both of them.

UINavigationController's back button disappears?

I notice something strange happens to one of my view controller: the back button disappears, yet it's possible to go back to previous view controller by tapping the top left corner (i.e where the button should reside).
In my entire file there's no line that set self.navigationItem.hidesBackButton to YES; also NSLog prints 0 as self.navigationItem.hidesBackButton's value in viewDidLoad.
This occurs in both the simulator and real device. Any ideas?
Oh dear. In the implementation of the previous view controller, I accidentally set self.title to #"", which causes this annoying bug.
Remove this line solves the problem.
I had a recursive navigation controller, and this also happened to me, I used this code to fix it:
self.navigationItem.leftItemsSupplementBackButton = true
Just in case someone is facing this issue with a custom back button and the above fixes did not work, here is a similar issue I faced with a different solution.
I had a customized back button with text that was disappearing while the arrow could be seen UINavigationController custom back button disappears from NavigationBar
So if anyone is facing a similar situation with disappearing back button text on a customized back button, here is my scenario and fix.
I customized my back button inside a custom NavigationController class as follows:
private func customizeBackButton() {
let backImage = UIImage(named: "BackButton")?.withRenderingMode(.alwaysOriginal)
navigationBar.backIndicatorImage = backImage
navigationBar.backIndicatorTransitionMaskImage = backImage
UIBarButtonItem.appearance().setTitleTextAttributes([
NSAttributedString.Key.foregroundColor: UIColor.panoStoryYellow,
NSAttributedString.Key.font: UIFont(name: "Montserrat-SemiBold", size: 15)!
], for: .normal)
}
This gave me:
Now when I tapped on the back button text, the text disappeared:
I made sure that I followed all the above answers such as setting titles making sure the tint color is valid etc. however this did not work.
In my case, I needed to set attributes even for the highlighted state of the back button as follows:
UIBarButtonItem.appearance().setTitleTextAttributes([
NSAttributedString.Key.foregroundColor: UIColor.panoStoryYellow,
NSAttributedString.Key.font: UIFont(name: "Montserrat-SemiBold", size: 15)!
], for: .highlighted)
After this, the back button text never disappeared