Trying to change icon color in UIAction but changing tint does not seem to work at all. Any idea?
let imageView = UIImage(systemName: "eye")!
.withTintColor(.red, renderingMode: .alwaysTemplate)
The source code below is from Apple "Adding Menus and Shortcuts to the Menu Bar and User Interface" example, only imageView
is the new element.
func contextMenuActions() -> [UIMenuElement] {
let imageView = UIImage(systemName: "eye")?.withTintColor(.red, renderingMode: .alwaysTemplate)
// Actions for the contextual menu, here you apply two actions.
let copyAction = UIAction(title: NSLocalizedString("CopyTitle", comment: ""),
image: imageView,
identifier: UIAction.Identifier(rawValue: "com.example.apple-samplecode.menus.copy")) { action in
// Perform the "Copy" action, by copying the detail label string.
if let content = self.detailItem?.description {
UIPasteboard.general.string = content
}
}
You need to use rendering mode .alwaysOriginal
because internally they use UIImageView
, which applies own tintColor
for all template image.
So making
func contextMenuActions() -> [UIMenuElement] {
let imageView = UIImage(systemName: "eye")?.withTintColor(.red,
renderingMode: .alwaysOriginal) // << here !!
gives
Tested with Xcode 12.1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With