【问题标题】:How to change icon color in UIAction inside UIMenu? [duplicate]如何在 UIMenu 内的 UIAction 中更改图标颜色? [复制]
【发布时间】:2020-11-06 12:44:54
【问题描述】:

尝试在 UIAction 中更改图标颜色,但更改色调似乎根本不起作用。有什么想法吗?

let imageView = UIImage(systemName: "eye")!
                    .withTintColor(.red, renderingMode: .alwaysTemplate)

下面的源代码来自苹果"Adding Menus and Shortcuts to the Menu Bar and User Interface"的例子,只有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
                                         }
        }

【问题讨论】:

    标签: ios swift uikit


    【解决方案1】:

    您需要使用渲染模式.alwaysOriginal,因为它们在内部使用UIImageView,它为所有模板图像应用自己的tintColor

    所以制作

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

    给予

    使用 Xcode 12.1 测试

    【讨论】:

    • 一如既往的好答案!谢谢,阿斯佩里。
    猜你喜欢
    • 1970-01-01
    • 2021-12-17
    • 2014-01-13
    • 2020-12-26
    • 2019-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多