【问题标题】:SwiftUI Picker item .foregroundColor() not workingSwiftUI 选择器项目 .foregroundColor() 不起作用
【发布时间】:2021-01-12 00:36:19
【问题描述】:

我正在尝试在 Picker 中设置文本的颜色,但它实际上并没有改变任何东西。以下是我的代码:

Picker("Theme Color", selection: $themeColor) {
    ForEach(0..<themeColors.count) { _ in
        Text("Color").foregroundColor(.blue)
    }
}.pickerStyle(SegmentedPickerStyle())

这在 SwiftUI 中是不可能的吗?

【问题讨论】:

    标签: swift xcode swiftui


    【解决方案1】:

    我设法change 选择器的前景色,但这远非好,因为所选项目不可读。 考虑到我在 iOS 13 上使用分段选择器的不幸经历,以及当时我无法更改选择器大小的事实,我确实认为选择器仍然无法自定义,就像我一样,如果你需要编写自己的选择器你想要一个更个性化的。

    【讨论】:

      【解决方案2】:

      嗯...没有什么直接的,但是您可以使用以下方法...可以获得一些有趣的效果(也许有人可能合适)

      使用 Xcode 12 / iOS 14 准备和测试的演示

          Picker("Theme Color", selection: $themeColor) {
               ForEach(0..<4) { _ in
                    Text("Color")
               }
          }.pickerStyle(SegmentedPickerStyle())
          .colorMultiply(.white).colorInvert()
      

      甚至

          Picker("Theme Color", selection: $themeColor) {
               ForEach(0..<4) { _ in
                    Text("Color")
               }
          }.pickerStyle(SegmentedPickerStyle())
          .colorMultiply(.white).colorInvert()
          .colorMultiply(.orange).colorInvert()
      

      【讨论】:

        【解决方案3】:

        在 SwiftUI 2 中,Picker 仍由 UISegmentedControl 支持。

        这意味着您可以使用UISegmentedControl.appearance() 修饰符:

        UISegmentedControl.appearance().selectedSegmentTintColor = .systemOrange
        UISegmentedControl.appearance().backgroundColor = .systemRed
        

        【讨论】:

        • 这让我很接近,但有没有办法单独更改每个?
        • @JoeScotto 您可以单独设置背景颜色,但我不知道所选项目的解决方案(您可以随时尝试 Asperi 的解决方案)。
        • 你会如何处理每个项目?选中的我只想要边框什么的。
        • @JoeScotto 您可以使用UISegmentedControl.appearance().backgroundColor = .clear,然后将background(Color.red) 修饰符应用于您的选择器。但我不知道如何在 SwiftUI 中访问所选段的外观。
        • @JoeScotto 您找到单独更改行颜色的解决方案了吗?
        猜你喜欢
        • 2013-03-22
        • 2021-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-22
        相关资源
        最近更新 更多