I'm working on a SwiftUI app that needs a TextField that dynamically expands as the user types but with a maximum number of lines. I want it to:
Start with a single line Grow as the user types until reaching max lines Then become scrollable once max lines is reached
Here's my current implementation, but it doesn't handle the height correctly:
struct ContentView: View {
@State private var text = ""
var body: some View {
VStack {
TextField("Enter your text here", text: $text)
.padding()
.frame(minHeight: 40)
.background(Color.gray.opacity(0.2))
.cornerRadius(8)
.lineLimit(5)
}
.padding()
}
}
The TextField doesn't grow properly, and once the text exceeds the visible area, it just clips instead of becoming scrollable. How can I implement this correctly in SwiftUI?