I would like to have a ScrollView that encloses a Table and a TextEditor, stacked vertically. With this arrangement, a user would always scroll down past all the table rows to find the text editor.
Below is modified Apple example code that I've been experimenting with to no avail. I get the desired stacking by manually adjusting tableContentHeight variable. How could I adjust it dynamically at runtime? Or is there some other way to achieve the desired setup?
import SwiftUI
struct Person: Identifiable {
let givenName: String
let familyName: String
let emailAddress: String
let id = UUID()
}
struct PeopleTable: View {
@State private var people = [
Person(givenName: "Juan", familyName: "Chavez", emailAddress: "[email protected]"),
Person(givenName: "Mei", familyName: "Chen", emailAddress: "[email protected]"),
Person(givenName: "Tom", familyName: "Clark", emailAddress: "[email protected]"),
Person(givenName: "Gita", familyName: "Kumar", emailAddress: "[email protected]"),
Person(givenName: "Juan", familyName: "Chavez", emailAddress: "[email protected]"),
Person(givenName: "Mei", familyName: "Chen", emailAddress: "[email protected]"),
Person(givenName: "Tom", familyName: "Clark", emailAddress: "[email protected]"),
Person(givenName: "Gita", familyName: "Kumar", emailAddress: "[email protected]")
]
// TODO: make this track table content height
@State private var tableContentHeight: CGFloat = 235
@State private var text: String = loremIpsum
var body: some View {
ScrollView {
Table(people) {
TableColumn("Given Name", value: \.givenName)
TableColumn("Family Name", value: \.familyName)
TableColumn("E-Mail Address", value: \.emailAddress)
}
.frame(height: tableContentHeight)
TextEditor(text: $text).font(Font.system(size: 14))
}
}
}
#Preview {
PeopleTable()
}
let loremIpsum = """
Pariatur neque enim occaecati ut inventore odio. Eius earum cumque ea placeat debitis nobis eos. Doloremque eum consectetur quos quia. Optio dignissimos quis iste. Nostrum animi vel doloribus et. Non delectus odio dolor in.
Eos ea aperiam est cupiditate eius quibusdam nulla. Sed sed aut labore blanditiis temporibus dolores. Doloribus vel expedita et possimus rerum. Doloribus ut vel nisi debitis molestias.
Pariatur quia eligendi et. Ipsa quae enim fugiat modi odit voluptatibus est. Officiis non impedit repellat omnis quod labore. Ratione ut iste ab ratione atque atque.
Quos nesciunt aut quos autem aut iusto repellendus. Error ex labore ad eaque dolorem. Laborum sed et ut incidunt mollitia omnis molestias. Reiciendis officiis alias quos expedita asperiores et culpa. Reiciendis nihil minima qui consequuntur. Maiores sint ut numquam facere ullam temporibus ea officiis.
"""
Listor just aScrollViewfor that. there isn't a built in way yet for this.