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

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;

Related

changing image color when clicked

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)

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

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.

UISlider with custom UIImage set to UIImageRenderingModeAlwaysTemplate

I'm trying to customise the look of a UISlider by setting custom images for the thumb, minimumTrack and maximumTrack like so:
let sliderThumbImage = UIImage(named: "slider-thumb")
volumeSlider.setThumbImage(sliderThumbImage, forState: .Normal)
let minTrackImage = UIImage(named: "slider-min-track")
volumeSlider.setMinimumTrackImage(minTrackImage, forState: .Normal)
let maxTrackImage = UIImage(named: "slider-max-track")
volumeSlider.setMaximumTrackImage(maxTrackImage, forState: .Normal)
This works as expected.
However, I would like to set these images to be template images, by setting their rendering mode to UIImageRenderingModeAlwaysTemplate
The problem is that if I then set the tintColor like this:
volumeSlider.thumbTintColor = UIColor.greenColor()
then the custom image is removed and the default image is used again.
How can I customise the image, and the tintColor simultaneously?
Edit: Docs seem to support the problem I'm having:
Note that you can only adjust the tint of the default track and thumb
images, not custom images. Setting the tint of a part of the slider
that has custom images associated with it will remove those images.
Source: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/UIKitUICatalog/UISlider.html
If you set the tint color on the UISlider itself, it will change the tint of your custom thumb image.
volumeSlider.tintColor = .green
You do however need to create the thumb image with its rendering mode set to alwaysTemplate. This can be done thru the asset manager or in code.

Resources