More example here:
//
// ListSafeAreaBarKeyboardTextField.swift
// Exploration
import SwiftUI
import Foundation
struct ListSafeAreaBarKeyboardTextField: View {
@State private var typedText: String = ""
@FocusState private var focusingTextField: Bool
private let items = Array(1...16)
var body: some View {
ScrollViewReader { proxy in
List {
ForEach(items, id: \.self) { number in
Text("Item \(number)")
.id(number)
}
Text("Scroll to This")
.id("ScrollToThis")
}
.defaultScrollAnchor(.bottom)
.onAppear {
if let lastItem = items.last {
proxy.scrollTo(lastItem, anchor: .bottom)
}
}
.onChange(of: focusingTextField) { oldValue, newValue in
// if TextField is focused
if newValue == true {
// We scroll to the known last item ("ScrollToThis")
// ❌ (Somehow) This simply never works
withAnimation {
proxy.scrollTo("ScrollToThis", anchor: .bottom)
}
}
}
}
.scrollDismissesKeyboard(.interactively)
.safeAreaBar(edge: .bottom) {
TextField("Type here", text: $typedText, axis: .vertical)
.focused($focusingTextField)
// Design
.padding(.horizontal, 16)
.padding(.vertical, 10)
.glassEffect()
// Paddings
.padding(.horizontal, 24)
.padding(.vertical, 16)
}
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: