swiftuiuitextfieldswiftui-text

change color of uitextfield not working in swiftui


I am trying to change color of textfield with below code but it is not working. It takes color as white if the it is light theme and for dark theme automatically takes black color. I want to keep it always black.

    var body: some View {

    TextField(self.textFieldTitle ?? "", text: self.$textFieldInput, onEditingChanged: { editing in self.dosomeCode()}, onCommit: { self.someCode()})
                .background(Color.green)
                .foregroundColor(Color.white)
}

Solution

  • I want to keep it always black.

    Use .black color instead

    TextField(self.textFieldTitle ?? "", text: self.$textFieldInput, onEditingChanged: { editing in self.dosomeCode()}, onCommit: { self.someCode()})
                .background(Color.green)
                .foregroundColor(Color.black)  // << here !!
    

    demo1demo2

    Tested with Xcode 13.2 / iOS 15.2