为什么我的TextField的背景没有更改为我设置的颜色?

时间:2020-03-06 23:40:34

标签: swiftui

TextField remains white even when background colour is set

为什么在将TextField设置为使用定义的颜色后,TextField的背景仍保持白色?为什么仍然覆盖白色?

import SwiftUI

struct ContentView: View {
    @State var search: String = ""

    var body: some View {
        ZStack{
            Color.baseColour
            .edgesIgnoringSafeArea(.all)

            VStack(alignment: .center, spacing: 0) {
                HStack(alignment: .center, spacing: 0) {
                    TextField("Search for a profile... ", text: $search)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                        .cornerRadius(15)
                        .background(Color.textFieldBarColour)
                }
                .frame(width: UIScreen.main.bounds.width - 30)
                .background(Color.textFieldBarColour)}

1 个答案:

答案 0 :(得分:1)

可以使用纯文本字段样式,如下所示(您的自定义颜色刚刚被替换为演示用)

demo

TextField("Search for a profile... ", text: $search)
    .textFieldStyle(PlainTextFieldStyle())
    .padding(8)
    .background(RoundedRectangle(cornerRadius: 15).fill(Color.yellow))