I have a text field in swiftUI, and in order to make it more appealing I'd like to add a border and have rounded corners. But it doesn't seem to work like it's supposed to (see image). What did I miss?
I've tried putting .cornerradius() before .border(), but it had the same effect.
TextField("Text input goes here", text: $addMins)
.padding(.all, 5.0)
.background(View)
.frame(width: 300.0, height: 35.0)
.border(Color.green, width: 2)
.cornerRadius(14)
Rounded border around Text view in SwiftUI A default, rectangular border can be added to a stack view using the.border () modifier: VStack { Text ("Simple Swift Guide"). font (. largeTitle) Text ("SwiftUI Tutorials"). font (. title). foregroundColor (. gray) }. padding (). border (Color. black, width: 4)
Add rounded border to a Text view using .overlay () modifier and RoundedRectangle shape with a custom stroke width: A default, rectangular border can be added to a stack view using the .border () modifier: Remember that in order to add a border with rounded corners to a stack view, you need to use the method with .overlay () modifier.
Before the release of Xcode 11 beta 6, you can use the border modifier and pass it with the corner radius: However, the latest beta of Xcode 11 has deprecated the function call. To create a border with rounded corners, you can draw a rounded rectangle and overlay on the button like this:
Create rectangular or rounded-corners borders using .border () or .overlay () modifiers respectively. Create solid line or dashed borders. Add default, rectangular border to a Text view using .border () modifier:
Here a simple way:
struct ContentView: View {
@State private var stringOfTextField: String = String()
var body: some View {
TextField("Enter text . . .", text: $stringOfTextField)
.padding()
.overlay(RoundedRectangle(cornerRadius: 10.0).strokeBorder(Color.black, style: StrokeStyle(lineWidth: 1.0)))
.padding()
}
}
So you want something like this?
TextField("Text Field", text: $text)
.padding(4)
.overlay(
RoundedRectangle(cornerRadius: 14)
.stroke(Color.green, lineWidth: 2)
)
.padding()
Here a simple way:
struct ContentView: View {
@State private var stringOfTextField: String = String()
var body: some View {
TextField("Enter text . . .", text: $stringOfTextField)
.padding()
.overlay(RoundedRectangle(cornerRadius: 10.0).strokeBorder(Color.black, style: StrokeStyle(lineWidth: 1.0)))
.padding()
}
}
By the way, if you need a stroked and corner radiused text field with a different background, here's a solution
TextField("Placeholder", text: $text)
.padding()
.background(RoundedRectangle(cornerRadius: 5).fill(Color.gray))
.overlay(
RoundedRectangle(cornerRadius: 5)
.stroke(lineWidth: 1)
)
.foregroundColor(.black)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With