TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

iOS 16.4+, SwiftUI, TextField always on top of keyboard

Forums > SwiftUI

I've been playing around with SwiftUI List view where each "cell" contains a multiline TextView.

When user selects a TextView at the bottom of the view, the chosen "cell" with TextView scrolls upward to stay visible above keyboard (as expected).

However, I've encountered an issue: when a user enters more text that extends beyond a single line, the TextField doesn't scroll up. Consequently, the text remains hidden from view.

import SwiftUI

struct PlaygroundView: View {

    let sections = ["Section 1", "Section 2", "Section 3", "Section 4", "Section 5", "Section 6"]
    @State private var sectionText = Array(repeating: "", count: 6)

    var body: some View {
        List {
            ForEach(sections.indices, id: \.self) { index in
                Section(header: Text(sections[index])) {
                    TextField("Enter text", text: $sectionText[index], axis: .vertical)
                }
            }
        }
    }
}

Is there an out of the box solution available to ensure that a TextField remains consistently visible on top of the keyboard?

Video: https://imgur.com/a/RgMQksp

Another example is Apple build-in contacts app -> Edit contact -> Notes field

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.