changing image color when clicked - ios

I have for example this icon:
this is the color of the icon when its not clicked .. when i click it it will change the color as shown below:
what is the best way to achieve that? is there is a way to do it as a font icon and change the color of it? or shall i just change the image when clicked and unclicked?
can someone please tell me the best way to do it...

Use rendered image
var renderedIcon: UIImage? = UIImage(named:"myImage")?.withRenderingMode(.alwaysTemplate )
imageView.image = renderedIcon
imageView.tintColor = //Normal Color
Change the tintColor when button clicked.
OR
You could set
myButton.setImage(UIImage(named : "unselectedImage"), forState: UIControlState.Normal)
myButton.setImage(UIImage(named : "selectedImage"), forState: UIControlState.Selected)

Related

Is there any way to use "withRenderingMode(.alwaysTemplate)" with default image by not adding any tint colour of a button?

I want to use "withRenderingMode(.alwaysTemplate)" to set an image on a button, but don't want to change any tint colour. Just want to set the default image.
let button = UIButton(type: .custom)
let image = UIImage(named: "image_name")?.withRenderingMode(.alwaysTemplate)
button.setImage(image, for: .normal)
button.tintColor = UIColor.red
Here I didn't want to use red tint colour. But if though I didn't use any tint colour, I didn't get actual image. I just want to set actual image.
If the image is already colored the way you like, then you could leave off the
withRenderingMode(.alwaysTemplate)
From documentation, Apple states that UIImage.RenderingMode.alwaysTemplate will:
Always draw the image as a template image, ignoring its color information.
Also, to ensure you get your original color you could use:
withRenderingMode(.alwaysOriginal)
Back to the documentation:
Always draw the original image, without treating it as a template.

Find text color of button in swift4

I am working on app and I give UIButton text color from storyboard (Different for default and selected), I want both text color in a variable.
Anyone help me with this problem.
I have one solution for this problem " Take 'color code' of this color and give it to variable"
Anyone has any other short method for this problem.
Because if I use 'color code' then in future when I change color of button text then Then also work on that 'color code'.
You need to call titleColor() method of UIButton
let color = btnOutletExample.titleColor(for: .normal) or your desired state
try this
let titleColor: UIColor = yourButton.currentTitleColor
At the end of viewDidLoad, you can add the following to capture the color information:
let defaultTextColor = myButton.titleColor(for: .normal)
let selectedTextColor = myButton.titleColor(for: .selected)
You could pull the colour out of the element you configured in your storyboard, using the following code.
let btnTextColor = self.myBtnOutlet.titleLabel.titleColor(for: .normal)

UIButton's image shows up faint?

I'm trying to change UIButton's image to white and set a background color when it's pressed to end up with something like this:
I'm setting the button's image to a white one and setting it's background color like this:
button.setImage(UIImage(named: "Food_White")?.withRenderingMode(.alwaysOriginal), for: .highlighted)
button.setBackgroundImage(UIImage.imageWithColor(UIColor.gray, size: CGSize(width: 1.0, height: 1.0)), for: .highlighted)
But when I press the button the white image barely shows:
Does anybody know how to make the image less faint?
Are you using the default button type when creating them?
Try creating the button with .custom type instead of the default .system

How to set clean Image on Button in ios(Xamarin)

I am new to Xamarin and iOS. I am setting Image on Button via both the way design and programatically.
Design Way :
my Image name is Pen.png.
Programatically Way :
btnEditAccount.SetImage(UIImage.FromFile("Pen.png"), UIControlState.Normal);
But in Both case My Image not set well. It Complete White Image. But on Button it display below Image.
Output :
I Expect :
Is there any Property I miss for my understing ?
How to set full white Image on Button.
Any Help be Appreciated.
button.setImage(UIImage.init(named: "icon (5).png"), forState: .Normal)
button.tintColor = UIColor.clearColor()
Finally I set TintColor to Button Image and it will solve the Probelm.
Programatically this way
btnEditAccount.SetImage(UIImage.FromFile("Pen.png"), UIControlState.Normal);
btnEditAccount.TintColor = UIColor.White;

Icon color in MK FabButton

I'm using the MK pod (https://github.com/CosmicMind/MaterialKit) to try and place a fab button in my layout. The image I'm using is black. But the fab button appears to be overriding my icon color with a default color, because it shows up a faint blue color in the final fab button. Below are screenshots of the original image and the fab button with the image applied. Below are the only two lines I'm using to customize the fab button (I've subclassed it and made it IBDesignable, and the color I'm applying is one I've defined in a UIColor extension).
backgroundColor = UIColor.customBlueColor()
setImage(UIImage(named: "wifi"), forState: .Normal)
And here are the original icon and a screenshot of the result:
How do I keep the original icon color?
try using the tintColor property to adjust the icon color.
Example:
let img: UIImage? = UIImage(named: "wifi")
fabButton.setImage(img, forState: .Normal)
fabButton.setImage(img, forState: .Highlighted)
fabButton.tintColor = UIColor.blackColor()
Also, to avoid the black highlight color that occurs when you press the FabButton, set the image to .Highlighted, as well as, .Normal.

Resources