【问题标题】:Can you customize the activation highlight color of a SwiftUI context menu?您可以自定义 SwiftUI 上下文菜单的激活突出显示颜色吗?
【发布时间】:2019-12-12 21:05:13
【问题描述】:

我有一个深色背景上带有浅色文本的视图。我需要向它添加一个上下文菜单。这是我正在使用的代码:

Text(rowModel.detail)
     .font(detailFont)
     .foregroundColor(Color(/* a light color */))
     .contextMenu {
     Button(action: {
         UIPasteboard.general.string = self.rowModel.detail
     }, label: {
         Text("Copy")
     })
 }

当菜单被激活并且设备处于光照模式时,结果如下:

我在网上找不到任何东西表明我可以更改突出显示的颜色。但以防万一,我想我会在这里问。可以做到吗?谢谢!

【问题讨论】:

  • 您是否偶然找到了解决方案?

标签: ios swift swiftui


【解决方案1】:

您可以这样做为您的按钮设置自定义突出显示状态

struct YourButtonStyle: ButtonStyle {

  func makeBody(configuration: Self.Configuration) -> some View {
    configuration.label
      .padding()
      .foregroundColor(.white)
      .background(configuration.isPressed ? Color.green : Color.blue)
      .cornerRadius(8.0)
  }

}


// To use it
Button(action: {
    UIPasteboard.general.string = self.rowModel.detail
}, label: {
    Text("Copy")
})
.buttonStyle(YourButtonStyle())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-14
    • 2011-12-19
    • 1970-01-01
    • 2022-07-21
    • 2019-09-05
    • 2020-09-05
    • 2022-07-01
    • 1970-01-01
    相关资源
    最近更新 更多