如何在 UIMenu 内的 UIAction 中更改图标颜色?

Lor*_*lor 4 uikit ios swift

尝试在 UIAction 中更改图标颜色但更改色调似乎根本不起作用。任何的想法?

let imageView = UIImage(systemName: "eye")!
                    .withTintColor(.red, renderingMode: .alwaysTemplate)
Run Code Online (Sandbox Code Playgroud)

下面的源代码来自Apple “将菜单和快捷方式添加到菜单栏和用户界面”示例,只是imageView新元素。

    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
                                         }
        }
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Asp*_*eri 8

您需要使用渲染模式,.alwaysOriginal因为它们在内部使用UIImageView,这适用tintColor于所有模板图像。

所以制作

func contextMenuActions() -> [UIMenuElement] {
    let imageView = UIImage(systemName: "eye")?.withTintColor(.red, 
          renderingMode: .alwaysOriginal)                            // << here !!
Run Code Online (Sandbox Code Playgroud)

给

演示

使用 Xcode 12.1 测试