我正在尝试将背景颜色设置为我的文本字段,它似乎为文本字段周围的填充区域设置了很好的设置,而不是设置到实际的文本字段。
struct PrimaryTextField: View {
var initialText: String
@Binding var key: String
var body: some View {
VStack {
TextField(initialText, text: $key)
.padding()
.font(.title3)
.frame(maxWidth: .infinity)
.background(Color("TextFieldBGColor"))
.cornerRadius(50.0)
.shadow(color: Color.black.opacity(0.08), radius: 60, x: 0.0, y: 16)
.accentColor(Color("AccentColor"))
.textFieldStyle(.roundedBorder)
}
}
}
如何使白色文本字段成为灰色文本字段(在光模式下)?
我尝试了前景色为TextFieldBGColor
,但它正在改变文本字段的颜色,我不想改变。
发布于 2022-05-02 02:12:02
SwiftUI的TextField
拥有一个TextFieldStyle
属性,该属性控制TextField
的UI的某些方面--这是负责边框的内容,并在TextView
上添加了背景。不确定这是否正是您想要的效果,但您可以简单地将修饰符.textFieldStyle
添加到您的TextField
中,以删除添加的样式,这将删除TextField
的背景色。如果您想要原来的边框,您可能必须使用.background
修饰符作为自定义添加该边框。
TextField(initialText, text: $key)
.textFieldStyle(.plain) <------ Added here
.padding()
.font(.title3)
.frame(maxWidth: .infinity)
.background(Color("TextFieldBGColor"))
.cornerRadius(50.0)
.shadow(color: Color.black.opacity(0.08), radius: 60, x: 0.0, y: 16)
.accentColor(Color("AccentColor"))
.textFieldStyle(.roundedBorder)
在这里阅读更多关于TextFieldStyle
协议的内容:
https://developer.apple.com/documentation/swiftui/textfieldstyle
https://stackoverflow.com/questions/72081678
复制相似问题