简体   繁体   English

SwiftUI 选择器项目 .foregroundColor() 不起作用

[英]SwiftUI Picker item .foregroundColor() not working

I'm trying to set the color of the text within a Picker but it is not actually changing anything.我试图在 Picker 中设置文本的颜色,但它实际上并没有改变任何东西。 The following is my code:以下是我的代码:

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

Is this just not possible in SwiftUI?这在 SwiftUI 中是不可能的吗?

Well... nothing direct, but you can play with the following approach... which allows to get some interesting effects (maybe someone could be appropriate)嗯......没有直接的,但你可以使用以下方法......这可以获得一些有趣的效果(也许有人可能是合适的)

Demo prepared & tested with Xcode 12 / iOS 14使用 Xcode 12 / iOS 14 准备和测试的演示

演示1

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

and even乃至

演示2

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

backup 备份

I managed to change the foreground color of the picker, but it was far from good since the chosen item would not be readable.我设法change了选择器的前景色,但这远非好,因为所选项目不可读。 Considering my iOS 13 unfortunate experiences with segmented picker, and the fact that i couldn't change the picker's size at that time, i do think that pickers are still not customizable and just like me, you'll need to write your own picker if you want a more customized one.考虑到我在 iOS 13 上使用分段选择器的不幸经历,以及当时我无法更改选择器大小的事实,我确实认为选择器仍然无法自定义,就像我一样,如果你需要编写自己的选择器你想要一个更定制的。

In SwiftUI 2, Picker is still backed by UISegmentedControl .在 SwiftUI 2 中, Picker仍然由UISegmentedControl支持。

Which means you can use the UISegmentedControl.appearance() modifiers:这意味着您可以使用UISegmentedControl.appearance()修饰符:

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

我使用“setTitleTextAttributes”得到了想要的结果

UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.white], for: .normal)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM