SwiftUI Picker 自定义文本颜色很快就不起作用

发布于 2025-01-19 16:29:46 字数 609 浏览 6 评论 0原文

使用以下代码我正在创建一个选择器,但是自定义存在一个巨大的问题。

只有 .background(color.red)作品。

为什么对选择器感到痛苦。 Uiview Picker也是如此。

我找不到改变背景或文本颜色的方法。

Picker("what is this", selection: $selectedTray) {
                ForEach(trayType, id: \.self) {
                    Text($0)
                        .multilineTextAlignment(.trailing)
                        .foregroundColor(Color.green)
                        .background(Color.blue)
                        
                }
            }.foregroundColor(Color.white)
             .background(Color.red)
                

Using following code I am creating a picker but there is a huge problem with customization.

Only .background(Color.red) works.

Why it has be pain with Picker.
It was the same with UIView Picker as well.

I could not find a way to change background or text color.

Picker("what is this", selection: $selectedTray) {
                ForEach(trayType, id: \.self) {
                    Text($0)
                        .multilineTextAlignment(.trailing)
                        .foregroundColor(Color.green)
                        .background(Color.blue)
                        
                }
            }.foregroundColor(Color.white)
             .background(Color.red)
                

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

归途 2025-01-26 16:29:46

目前尚不清楚您想要什么定制。在一部分中,您指出仅设置 .background() 颜色有效,在另一部分中,您指出“我找不到更改背景或文本颜色的方法”。显然,您已经找到了如何更改背景颜色。

struct ItemPickerView: View {
    @State private var selectedItem = 1
    let cups: [Int] = Array(1..<21)
    var body: some View {
        Picker("Select Item: ", selection: $selectedItem){
            ForEach(Array(1..<21), id: \.self){ item in
                Text("Item \(item)")
            }
        }
        // Change the Text color with .accentColor()
        .accentColor(.red)
        // As you are aware, set a .background() for the selection background
        .background(Color.yellow)
    }
}

如果这不是您想要的,请发布图片或对您想要的内容进行很好的描述。

It is not clear what customizations you want. In one part you date that only setting the .background() color works, in another you state "I could not find a way to change background or text color." Obviously, you have found out how to change the background color.

struct ItemPickerView: View {
    @State private var selectedItem = 1
    let cups: [Int] = Array(1..<21)
    var body: some View {
        Picker("Select Item: ", selection: $selectedItem){
            ForEach(Array(1..<21), id: \.self){ item in
                Text("Item \(item)")
            }
        }
        // Change the Text color with .accentColor()
        .accentColor(.red)
        // As you are aware, set a .background() for the selection background
        .background(Color.yellow)
    }
}

If this is not what you want, please post an image or a very good description of what you do want.

吃颗糖壮壮胆 2025-01-26 16:29:46

表单
您需要应用listrowbackground设置背景颜色

Form {
    Picker(selection: $selection, content: {
        ForEach(items) { item in
            Text(item.title)
                .tag(item.id)
        }
    })
    .pickerStyle(.inline)
    .listRowBackground(Color.black) <-- Here
}

Inside a Form
You need to apply the listRowBackground to set the background color

Form {
    Picker(selection: $selection, content: {
        ForEach(items) { item in
            Text(item.title)
                .tag(item.id)
        }
    })
    .pickerStyle(.inline)
    .listRowBackground(Color.black) <-- Here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文