【发布时间】:2019-12-18 14:23:28
【问题描述】:
为了适应我的其他控件,我的 TextField 视图具有比文本本身更高的框架。
标准视图显示在带有自定义占位符的图像顶部。
当我点击 TextField 时,较小的文本区域本身具有灰色背景,如图底部所示。
如何将此文本背景设置为清晰的颜色?
struct TestView: View {
@State var city: String
@State var street: String
var body: some View {
VStack{
ZStack(alignment: .center) {
if city.isEmpty { Text("Bitte Namen eingeben")
.font(.system(size: 24, weight: .heavy, design: .default))
.foregroundColor(Color( red: 1.0, green: 0.0, blue: 0.0, opacity: 0.3))
}
TextField("", text: self.$city)
.textFieldStyle(CustomTFStyle())
}
ZStack(alignment: .center) {
if street.isEmpty { Text("Bitte Strasse eingeben")
.font(.system(size: 24, weight: .heavy, design: .default))
.foregroundColor(Color( red: 1.0, green: 0.0, blue: 0.0, opacity: 0.3))
}
TextField("", text: self.$street)
.textFieldStyle(CustomTFStyle())
}
}
}
}
public struct CustomTFStyle : TextFieldStyle {
public func _body(configuration: TextField<Self._Label>) -> some View {
configuration
.accentColor(.black)
.frame(width: 300, height: 70, alignment: .center)
.border(Color.gray, width: 4)
.font(.system(size: 30, weight: .heavy, design: .default))
.clipShape(RoundedRectangle(cornerRadius: 12))
.shadow(radius: 12)
.foregroundColor(.black)
}
}
【问题讨论】:
-
嗨 Reiner,我刚刚测试了你的代码 - 但从未见过你的灰色背景......我该如何重现它?
-
运行代码,点击其中一个文本字段并输入文本
-
你测试的是哪个 iOS 版本?
-
我假设您正在测试 13.2....在 13.0 和 13.3 中,灰色没有显示在您的示例中
-
用 iOS 13.0 测试