相关疑难解决方法(0)

无法在 SwiftUI 中的菜单内设置按钮标签的颜色

如果我在 SwiftUI (iOS) 中创建一个菜单,我无法设置里面按钮的颜色,例如:

Menu("Actions") {
    Button(action: { }) {
        Label("Whatever", systemImage: "pencil")
             .background(Color.red)  // does not work
    }
    .background(Color.red)           // does not work either
    .buttonStyle(RedButtonStyle())   // does not work either
}

struct RedButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label.foregroundColor(Color.red)
    }
}
Run Code Online (Sandbox Code Playgroud)

如果Label我使用Text, or Image(我知道这一点)而不是 ,它也不起作用。

有什么办法可以做到吗?

PS:还有另一个相关的SO问题,但它非常通用且范围更广。

ios swift swiftui swiftui-menu

10
推荐指数
1
解决办法
6295
查看次数

将系统映像用于静态UIApplicationShortcutItem

UIApplicationShortcutItems在Info.plist中指定主屏幕快速操作时,是否可以使用SF Symbols的系统映像?

注意到可用iOS密钥文档除了指定了UIApplicationShortcutItemIconType诸如中的预定义枚举案例之一外,没有指定执行此操作的密钥UIApplicationShortcutIconTypeSearch

通过新的初始化程序创建动态快速动作时,可以使用系统映像UIApplicationShortcutIcon.init(systemImageName: String)。似乎很奇怪,它无法用于静态快速动作。

ios ios13 sf-symbols

6
推荐指数
2
解决办法
56
查看次数

如何在 SwiftUI 中更改 pdf 图标的颜色

我将 SF 符号导出为 pdf 图标,并将默认白色更改为黑色。我想将黑色改回白色。我发现这样.colorInvert做可以,但是当我放入ImageButton时又变黑了。

import SwiftUI

#if os(macOS)
extension Image {
    static func sfSymbol(_ systemName: String) -> some View {
        Image(systemName)
        .resizable()
        .aspectRatio(contentMode: .fit)
        .colorInvert()
        .frame(height: 20)
    }
}

struct ImageView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            Image.sfSymbol("square.and.arrow.down.fill")
            Button(action: {}, label: { Image.sfSymbol("square.and.arrow.down.fill") })
        }
    }
}
#endif
Run Code Online (Sandbox Code Playgroud)

PDF 图标:square.and.arrow.down.fill.imageset

在此输入图像描述

swiftui

3
推荐指数
1
解决办法
2275
查看次数

标签 统计

ios ×2

swiftui ×2

ios13 ×1

sf-symbols ×1

swift ×1

swiftui-menu ×1