Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

uikit

swift

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

like image 973
X.Creates Avatar asked Nov 06 '20 12:11

X.Creates


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

like image 93
Asperi Avatar answered Oct 28 '22 16:10

Asperi