尝试在 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)
您需要使用渲染模式,.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 测试
| 归档时间: |
|
| 查看次数: |
680 次 |
| 最近记录: |