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)
Related
in iOS 13 apps, I used the following snippet (placed it in SceneDelegate) to set a custom back image for navigation views (SwiftUI views) which worked perfectly:
UINavigationBar.appearance().backIndicatorImage = UIImage(named: "myBackImg")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = image
UINavigationBar.appearance().tintColor = .gray
/// removing back text from back button (will affect also the text in any button in the nav bar if exists)
UIBarButtonItem
.appearance(whenContainedInInstancesOf: [UINavigationController.self])
.setTitleTextAttributes([.foregroundColor: UIColor.clear], for: .normal)
UIBarButtonItem
.appearance(whenContainedInInstancesOf: [UINavigationController.self])
.setTitleTextAttributes([.foregroundColor: UIColor.clear], for: .highlighted)
but unfortunately, this doesn't work with iOS 14 devices using Xcode 12.0.1 which doesn't show the custom back image.
is there any way to set a custom back image for the back button item for all SwiftUI views in the whole app at once on iOS 14 ?
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)
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)
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)
In many games and apps I have seen when you click on something, the button image change and while your finger is still on the botton and move it from the button, the image change again and the button is not clicked
In spriteKit I normaly do
gave the button a unique name
in touchesBegan I use nodeInPosition and then check the name of the node
if the names match some action is run
In ViewController
creat the button in the interface builder
creat outline and IBAction and inside of the method some action is run
if the button is made programmatically I use addTarget()
But I don't know how to animate the button when is pressed/hovered and release(return to default state) when the finger is not on button
You can set two images to the button for the highlighted and normal state like this
button.setImage(imageUnhighlighted, forState: UIControlState.Normal)
button.setImage(imageHighlighted, forState: UIControlState.Highlighted)