SwiftUI:如何设置文本字段的背景色

时间:2020-10-20 01:49:23

标签: ios swift swiftui

description

当我选择文本字段时,它会摆脱其背景颜色(应为文本所在的位置)。例如,我附上一张显示持卡人文本字段的图像。也许只是一个错误?这是我的“ CardHolderView”代码:

再现错误:

struct ContentView: View {
    init() {
        UINavigationBar.appearance().barTintColor = #colorLiteral(red: 0.9384715557, green: 0.9561783671, blue: 1, alpha: 1)
        UINavigationBar.appearance().shadowImage = UIImage()
        UIScrollView.appearance().backgroundColor = #colorLiteral(red: 0.9384715557, green: 0.9561783671, blue: 1, alpha: 1)
    }
    
    @State private var name = ""
    
    var body: some View {
        NavigationView {
            ScrollView(UIScreen.main.bounds.height < 750 ? .vertical : .init()) {
                ZStack {
                    Color(#colorLiteral(red: 0.9384715557, green: 0.9561783671, blue: 1, alpha: 1)).edgesIgnoringSafeArea(.all)
                    CardHolderView(name: $name)
                        .padding()
                }
                .padding(.top, 60)
                .navigationBarTitle("Payment data", displayMode: .inline)
                .navigationBarItems(leading: Button(action: {}) { Image("left-arrow") })
            }
            .edgesIgnoringSafeArea(.all)
        }
    }
}

struct CardHolderView: View {
    @Binding var name: String
    
    var body: some View {
        VStack(alignment: .leading) {
            Text("Card holder")
            TextField("Your name and surname", text: $name)
                .padding()
                .background(Color.white)
                .cornerRadius(10)
        }
        .padding(.top)
    }
}

1 个答案:

答案 0 :(得分:1)

无需使用UIAppearance来设置ScrollView的背景颜色;您可以在其上使用.background修饰符。

UIAppearance进行了全局更改,并可能产生意想不到的后果。