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.
Related
This is a normal state of my UIButton:
But when I press on the UIButton, its highlight color should be changed to a different one. And here is what I have:
So, if you can notice, my white text becomes overlapped with the new color. But what I should have as a result is just a different color of the highlight and always the white text. Like so:
What I am doing so far is:
In Attribute Inspector in my xib file I changed the Highlight Color to a new one.
I also use updateButton.setTitleColor(.white, for: .highlighted) in my code but it doesn't help.
I've also taken a look at this question: UIButton background color overlaps text on highlight but the accepted answer didn't help much.
Similar to myButton.setTitleColor(.white, for: .normal) please try setting title colour for .highlighted state. That should work for you.
You can refer:
https://developer.apple.com/documentation/uikit/uibutton/1623993-settitlecolor
This myButton.setTitleColor(.white, for: .normal) doesnt change title color when highlighted , Its for normal state. If you want to change textColor when button is highlighted, you need to use myButton.setTitleColor(.white, for: .highlighted)
Also when you change state config in storyboard
you can set Text Color
If its a custom Button you can override isHighlighted method and do what you need like
class MyButton : UIButton {
override var isHighlighted: Bool{
didSet {
tintColor = isHighlighted ? UIColor.red : UIColor.white
backgroundColor = UIColor.blue
.....
// do what you need
}
}
As your button is subclass of UIView class CellButton: UIView {}, you need to handle touches begin and end events in custom CellButton and handle the appearance of the view. If it was subclass of UIButton then it would have handled automatically for you :)
You can also try with gestures, for more info you can refer: https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/handling_uikit_gestures/handling_tap_gestures
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.
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)
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
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.