我想做一个简单的表单并更改文本字段的背景颜色,但是xcode为我提供了.background(background: View)
等选项,但没有.background(Color()
)。
我想做一个简单的表单并更改文本字段的背景颜色,但是xcode为我提供了.background(background: View)
等选项,但没有.background(Color()
)。
Color
符合View
。因此,您可以像使用其他View
一样使用它。您的代码存在的问题是您在cornerRadius
修饰符内添加background
内部。应该是这样的:
TextField("Title",text: .constant("text"))
.background(Color.red)
.cornerRadius(5)
,
尝试以下代码。
struct ContentView: View {
@State var name: String = ""
var body: some View {
VStack(alignment: .leading,spacing: 10) {
Text("NAME").font(.headline)
TextField("Enter some text",text: $name)
.padding(.horizontal,15)
.frame(height: 40.0)
.background(Color(red: 239/255,green: 243/255,blue: 244/255))
.overlay(
RoundedRectangle(cornerRadius: 5)
.stroke(Color.gray.opacity(0.3),lineWidth: 1)
)
}.padding(.horizontal,15)
}
}
,
import SwiftUI
struct ContentView: View {
@State var name: String = ""
var body: some View {
VStack {
TextField("Enter some text",text: $name)
.background(Color.red)
}
}
}