SwiftUI 文本字段 - 如何在编辑模式下将文本字段的背景颜色设置为清除

纯渔夫

为了配合其他控件,我的文本字段获得了框架,其高度大于文本本身。标准视图显示在带有自定义占位符的示例上部文本字段中。当我单击 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)

    }
}
纯渔夫

我的 rootview 的 init 方法中的“UIScrollView.appearance().backgroundColor = .lightGray”也会影响 Textfield 的行为。删除这一行就成功了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章