Color Tint UIButton Image
As of iOS 7, there is a new method on UIImage
to specify the rendering mode. Using the rendering mode UIImageRenderingModeAlwaysTemplate
will allow the image color to be controlled by the button's tint color.
Objective-C
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [[UIImage imageNamed:@"image_name"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[button setImage:image forState:UIControlStateNormal];
button.tintColor = [UIColor redColor];
Swift
let button = UIButton(type: .custom)
let image = UIImage(named: "image_name")?.withRenderingMode(.alwaysTemplate)
button.setImage(image, for: .normal)
button.tintColor = UIColor.red
How to set the UIButton tint color of a system image when pressed?
You have to also disable adjusting image property:
buttonLogin?.adjustsImageWhenHighlighted = false
Now grey effect should be vanished
tintColor of UIButton's ImageView is not changing
Set Button type "System" and then set tint color.
This solution will definitely work. Please try this
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
[btn setTintColor:YOUR_COLOR];
[btn setImage:YOUR_Image];
Change tint image color when pressing uibutton in Swift
I don't know if is better approach or answer, but, i maybe could delivery this using this approach:
Create a method that will "fill" the color for your choice button and "clear" color to others , but its a method that loop through UIScrollView and look for each UIButton. Something like this :
func setBackgroundColorButton(color:UIColor , buttonTag:Int){
for view in self.scrollView.subviews as [UIView] {
if let btn = view as? UIButton {
if btn == buttonTag {
btn.tintColor = color
} else {
btn.tintColor = UIColor.whiteColor()
}
}
}
}
This is the concept, i didn't tested, but maybe just need adjust to search inside your scroll view or similar.
But with this will be work nice i believe :D
Change ImageView's tint color in UIButton based on state
check the below code:-
@IBAction func onClickButton(_ sender: UIButton) {
sender.imageView.image = sender.imageView.image?.withRenderingMode(.alwaysTemplate)
if sender.isSelected || sender.isHighlighted{
sender.imageView.tintColor = .red
}else{
sender.imageView.tintColor = .blue
}
}
UIButton image tint not working
Try setting the image to render as a template image. You can do this in your .xcasset folder selecting the image set, opening the attributes inspector and setting render as to "template image."
Changing color of image embedded to a button (Swift3)
Here's a simpler way to do this without any extension and without resetting the color on touch:
let stencil = myImage.withRenderingMode(.alwaysTemplate) // use your UIImage here
myButton.setImage(stencil, for: .normal) // assign it to your UIButton
myButton.tintColor = UIColor.white // set a color
Related Topics
Swift 3: Type 'Any' Has No Subscript Members
Programmatically Scroll a Uiscrollview
Custom Uitableviewcell Programmatically Using Swift
Xcode 11 Backward Compatibility: "Uiwindowscene Is Only Available in iOS 13 or Newer"
Wrapping Text in a Uitextview Around a Uiimage Without Coretext
How to Detect the Orientation of the Device on iOS
How to Round Corners of Uiimage with Hexagon Mask
Launchscreen.Xib Not Displaying My Custom Font
Xcode 6.3 Adding Margins to Tableviewcell
Write Extend File Attributes Swift Example
Firebase Cloud Messaging - Send Message to All Users
How to Create a New Swift Project Without Using Storyboards
Alternative iOS Layouts for Portrait and Landscape Using Just One .Xib File
Auto-Layout: What Creates Constraints Named Uiview-Encapsulated-Layout-Width & Height
Hiding Uitabbar When Pushing a Uiview
Xcode - Error Creating Lldb Target