UISlider with custom UIImage set to UIImageRenderingModeAlwaysTemplate - ios

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.

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.

Swift Changing UISlider images and tint doesn't work as expected

I have a slider in my ViewController:
#IBOutlet weak var timeSlider: UISlider!
When using the code:
timeSlider.setMinimumTrackImage(#imageLiteral(resourceName: "track"), for: .normal)
timeSlider.setMaximumTrackImage(#imageLiteral(resourceName: "track"), for: .normal)
timeSlider.setThumbImage(#imageLiteral(resourceName: "thumb2"), for: .normal)
My slider looks like this:
It works great and behaves as one would expect it to behave
Now I want to make the part before thumb have different color and the part after to also have different color, so I created a function which creates new images as templates (templates being an array) from images I use for my view:
func setTemplates(){
templates.removeAll()
for i in 0 ..< 10{
templates.append(playImg[i].withRenderingMode(.alwaysTemplate))
}
}
templates[5] is the same image as "thumb2"
templates[6] = "track"
and then I wanted to assign template[6] (which the image "track" converted to template) and color it in the same fashion I do with other images in the view like buttons, or to color labels etc. like this:
timeSlider.setMinimumTrackImage(templates[6], for: .normal)
timeSlider.setMaximumTrackImage(templates[6], for: .normal)
timeSlider.setThumbImage(templates[5], for: .normal)
timeSlider.minimumTrackTintColor = colors.primaryColor
timeSlider.maximumTrackTintColor = colors.detailColor
timeSlider.thumbTintColor = colors.secondaryColor
Where colors is a struct holding four different colors. But as you can see it doesn't work properly
The image thumb is being set to default one, it doesn't even behave properly as I can't move the thumb to 0.0 position of the slider and after I move it or the colors change, the part after thumb gets blue color which is default for slider.
I tried different ways to fight this problem but this is the closest I got it to working properly. At this point I'm starting to think it's a bug but maybe I made some mistake in my code. As I said I have buttons, labels, which I color the same way I tried here
button.setImage(templates[7], forState: .normal)
button.tintColor = colors.primaryColor
and it works
What am I doing wrong?
From the documentation for UISlider thumbTintColor:
Setting this property resets the thumb images back to the slider’s default images; any custom images are released by the slider. Setting this property to nil resets the tint color to the default and removes any custom thumb images.
Same for the two track tints.
If you want custom images then just set the custom images without also setting the tints.
In other words, set either the custom images or set the tints. Do not set both.

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