【问题标题】:Changing button color breaks formatting in Appkit SwiftUI在 Appkit SwiftUI 中更改按钮颜色会破坏格式
【发布时间】:2021-01-23 08:37:00
【问题描述】:

我是一名网络开发人员,正在尝试开发一个简单的 SwiftUI 应用程序,我在这里问之前用谷歌搜索了这个,但我仍然遇到了麻烦。

这是我正在做的一个小应用程序的 UI。我想改变我的按钮的颜色。

这是我在网上阅读了一些示例后尝试的代码:

Button(action: {
  self.device.setAsDefaultInputDevice()
  self.refresh()
}) {
  Text("Default")
    .padding()
    .background(Color.purple) // 2. Change the background color to purple including the padding
    .foregroundColor(.white)  // 3. Set the foreground/font color to white
    .font(.body)
}

当我向文本添加填充时,一切看起来都错了,我对默认按钮的形状完全没问题。 我的最终目的是根据默认值是否打开来交换主要/次要系统颜色。

【问题讨论】:

标签: swift swiftui appkit


【解决方案1】:
Button(action: {
  self.device.setAsDefaultInputDevice()
  self.refresh()
}) {
  Text("Default")
    .padding(.leading)
    .padding(.trailing)
    .padding(.top, 2)
    .padding(.bottom, 2)
    .background(RoundedRectangle(cornerRadius: 4).fill(Color.blue))
}

【讨论】:

  • 请考虑在您的答案中添加一些解释,以便其他人可以从中学习。
猜你喜欢
  • 2020-04-18
  • 2020-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-29
  • 2020-04-16
  • 1970-01-01
  • 2016-09-11
相关资源
最近更新 更多