【问题标题】:Why doesn't the background of my TextField change to the colour I've set?为什么我的 TextField 的背景没有变成我设置的颜色?
【发布时间】:2020-03-07 19:30:38
【问题描述】:

为什么在我将 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)}

【问题讨论】:

标签: swiftui


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多