'How to change icon color in UIAction inside UIMenu? [duplicate]

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
                                         }
        }

enter image description here



Solution 1:[1]

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

demo

Tested with Xcode 12.1

backup

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1