How to change icon color in UIAction inside UIMenu?

Viewed 2353

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

1 Answers

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

Related