如果我在 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问题,但它非常通用且范围更广。
UIApplicationShortcutItems
在Info.plist中指定主屏幕快速操作时,是否可以使用SF Symbols的系统映像?
注意到可用iOS密钥的文档除了指定了UIApplicationShortcutItemIconType
诸如中的预定义枚举案例之一外,没有指定执行此操作的密钥UIApplicationShortcutIconTypeSearch
。
通过新的初始化程序创建动态快速动作时,可以使用系统映像UIApplicationShortcutIcon.init(systemImageName: String)
。似乎很奇怪,它无法用于静态快速动作。
我将 SF 符号导出为 pdf 图标,并将默认白色更改为黑色。我想将黑色改回白色。我发现这样.colorInvert
做可以,但是当我放入Image
它Button
时又变黑了。
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)