I've png images in assets I've set Render As Template image. Following is the code. Why doesn't it set the image to be white? How to fix it?
#IBOutlet weak var iconImageView: UIImageView!
iconImageView.image = UIImage(named: "ico")?.withRenderingMode(.alwaysTemplate)
iconImageView.tintColor = .white
Your code is fine and it's working perfectly. The only problem is the image is having transparency that's why color is not visible properly.
it should work . try this, almost same.
extension UIImageView {
func setImageColor(color: UIColor) {
let templateImage = self.image?.withRenderingMode(UIImage.RenderingMode.alwaysTemplate)
self.image = templateImage
self.tintColor = color
}
}
Related
I'm using this code which works well on iPhone but I need to color a WKImage in WatchKit, how can I do this? I tried using the same code for an Extension on WKInterfaceImage but the image property is get only.
extension UIImageView {
func setImageColor(color: UIColor) {
let templateImage = self.image?.withRenderingMode(UIImage.RenderingMode.alwaysTemplate)
self.image = templateImage
self.tintColor = color
}
}
How can I color a UIImage in Swift?
I am using a UISegmentControl and am trying to give each segment's view a custom UIColor. This has been easy to do for solid colors. I simply do:
providerSegmentedControl.subviews[1].tintColor = UIColor.red
And the segment at index 1 will now be tinted with the color red.
However, I am trying to add a gradient for the tintColor of one of my segment's UIView. I have an image in my assets called "AppleMusicGradient" that looks like this:
I am trying to create a UIColor from this image using UIColor(patternImage:). The code looks like this:
//UIImage.resize is a UIImage extension function that simply resizes a UIImage to the given CGRect
let image = UIImage(named: "AppleMusicGradient")!.resize(targetSize: providerSegmentedControl.subviews[2].frame.size)
providerSegmentedControl.subviews[2].tintColor = UIColor(patternImage: image)
However, this method seems to only be partially working. When the given segment is selected, this gradient isn't visible at all. And when the given segment is unselected, the gradient is only visible on the label inside of the segment, i.e. the border normally seem is not present.
See the "Apple Music" segment.
My solution feels a little "hacky" but I believe this may be what you are looking for.
#IBOutlet weak var segmentedControl: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.updateGradientBackground()
}
private func updateGradientBackground() {
let sortedViews = segmentedControl.subviews.sorted( by: { $0.frame.origin.x < $1.frame.origin.x } )
for (index, view) in sortedViews.enumerated() {
if index == segmentedControl.selectedSegmentIndex {
view.backgroundColor = UIColor(patternImage: UIImage(named: "7Wk4B")!)
view.tintColor = UIColor.clear
let titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UISegmentedControl.appearance().setTitleTextAttributes(titleTextAttributes, for: .selected)
} else {
view.backgroundColor = UIColor.white
view.tintColor = UIColor.blue
}
}
}
#IBAction func segmentedControllerTapped(_ sender: Any) {
self.updateGradientBackground()
}
I'm using CarbonTabSwipeNavigation in my code (as follows). The issue is that the colour of all the icons is being updated automatically (probably to some default colour). How to fix that?
Apart from that, one of my icons is being updated (don't know why). I tried using the same icon in other parts of the application and it worked perfectly fine. Please help me in this. Thanks
weak var tab: CarbonTabSwipeNavigation!
override func viewDidLoad() {
super.viewDidLoad()
let iconNames = ["icn_events", "icn_places", "icn_activities", "icn_clubs"]
var images = [UIImage]()
for icon in iconNames {
if let img = UIImage(named: icon) {
images.append(img)
}
}
let carbonTabSwipeNavigation = CarbonTabSwipeNavigation(items: images, delegate: self)
carbonTabSwipeNavigation.pagesScrollView?.isScrollEnabled = false
carbonTabSwipeNavigation.insert(intoRootViewController: self)
//I'm using following piece of code to fix the color issue (please note, even without this, the image is being updated).
//carbonTabSwipeNavigation.setNormalColor(UIColor(hex: "5603ad"))
//carbonTabSwipeNavigation.setSelectedColor(UIColor(hex: "5603ad"))
carbonTabSwipeNavigation.setTabExtraWidth(45)
tab = carbonTabSwipeNavigation
}
I've attached the actual icon and the screenshot of how it's being displayed.
It was happening because of bar tint color. The issue was fixed the issue by image rendering mode:
let iconNames = ["icn_events", "icn_places", "icn_activities"]
var images = [UIImage]()
for icon in iconNames {
if let img = UIImage(named: icon) {
images.append(img)
}
}
if let img = UIImage(named: "icn_clubs")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) {
images.append(img)
}
So I am trying to give the user the ability to design their own virtual business card where the user can drag a label around the UIView, they can change background color of the UIView, as well as the color of the UILabel. The card itself is represented by a nib and the user selects the different color choices or template image choices through a UIPickerView. I want the Label to appear on top of the Image when it appears and is dragged to that part of the View, however it is not currently doing that. Changing colors of the background for the UIView and the text work just fine; I am also able to add and remove the Template image (which is a PNG) by using .isHidden().
My Problem is that when the UIImage is added to the UIView it is on top of the UILabel. If part of the UILabel overlaps with part of the png image it disappears behind it, and I would like it to appear over the image.
The code for the nib is in userCardView.swift and the picker view is on the mainViewController where I set the UserDefaults which works fine.
class userCardView: UIView {
#IBOutlet var cardView: UIView!
#IBOutlet weak var testLBL: UILabel!
#IBOutlet weak var templateImage: UIImageView!
var colorIndex = ["Black", "White", "Gray"]
var templateIndex = ["None", "Triangle"]
override func awakeFromNib() {
super.awakeFromNib()
cardView.clipToBounds = true
let backColorObject = UserDefaults.standard.object(forKey: "backGroundColor") as? String
let textColorObject = UserDefaults.standard.object(forKey: "textColor") as? String
let templateColorObject = UserDefaults.standard.object(forKey: "templateColor") as? String
let templateImageObject = UserDefaults.standard.object(forKey: "templateImage") as? String
for _ in templateIndex {
if templateImageObject == templateIndex[1] {
templateImage.isHidden = false
templateImage.image = UIImage(named: "test_triangle.png")
//testLBL.bringSubview(toFront: templateImage)
templateImage.bringSubview(toFront: testLBL)
} else if templateImageObject == templateIndex[0] {
templateImage.isHidden = true
} else {
templateImage.isHidden = true
}
}
I'm Still pretty new to using swift so any help at all would be greatly appreciated, thank you.
You need to ask testLBL's superview to bring it to the front. Your code templateImage.bringSubview(toFront: testLBL) won't work if templateImage is not the parent view of testLBL because testLBL is not in templateImage's view hierarchy.
So try to use the superview of testLBL
<testLBL's parent view>.bringSubview(toFront: testLBL)
or
testLBL.parentView.bringSubview(toFront: testLBL)
Hope it solves your problem
I have an image, inside a UIImageView, that comes with a default color, let's say a custom grey.
In my code at some point I set a different tint in this way
myImageView.image = myImageView.image!.withRenderingMode(.alwaysTemplate)
myImageView.tintColor = someCondition() ? UIColor.red : UIColor.lightGray
The fact is that I am not able to get back to the image's original grey color. My question is if there is a way to substitute the UIColor.lightGray part of my code with something telling the UIImageView not to use any tint, but its original color.
To not apply any tint you can set the image rendering mode like this:
image.renderingMode = .alwaysOriginal
In your code you could say:
let renderingMode: UIImageRenderingMode = condition ? .alwaysTemplate : .alwaysOriginal
myImageView.image = myImageView.image?.withRenderingMode(renderingMode)
I am using this approach to change/remove tint on tap
var imageView:UIImageView = {
let img = UIImageView()
img.image = someImage.withRenderingMode(.alwaysTemplate)
img.tintColor = .cyan
return img
}()
Add gesture recognizer to image view or button with action and then just assign image that is already assigned to image view but with different rendering mode
#objc func imgTap(){
imageView.image! = imageView.image!.withRenderingMode(.alwaysOriginal)
}