I use Rectangle()
to adding a bottom border on TextField (SwiftUI)
But I want to use protocol TextFieldStyle
for a bottom line on TextField Style like an RoundedBorderTextFieldStyle
How can I make a custom style for TextField without using Rectangle?
https://developer.apple.com/documentation/swiftui/staticmember
struct ContentView : View {
@State private var username = "Text Hellow"
var body: some View {
VStack() {
TextField($username)
.foregroundColor(Color.yellow)
Rectangle()
.frame(height: 1.0, alignment: .bottom)
.relativeWidth(1)
.foregroundColor(Color.red)
}
.padding()
}
func editChanged() {
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
Try it another way
LineView : Create custom wrappers for a
UIView
Conforming to the TextFieldStyle protocol
Usage:
To define a custom style, you can use the code below. The Rectangle is used eventually, but inside the custom style, not in your view code. Is this what you want?
Note: Tested on Beta 3
To use your custom initialiser:
Your custom initialiser implementation:
My solution is to add a divider under the textfield:
or use overlay
The solution of kontiki does not work with Beta 6, though. So I created a solution where I embed a TextField and a drawn bottom line in a new view. You can just copy the code and use it by writing
TextFieldWithBottomLine(placeholder: "My placeholder")
Used in a view it looks like:The first thing I create is a horizontal line:
Next I create a view that contains a TextField and the HorizontalLine below:
To see it in action I created a sample view:
Divider
A visual element that can be used to separate other content.
You can set
color
andheight