Skip to content

Instantly share code, notes, and snippets.

@levochkaa
Last active August 28, 2023 10:33
Show Gist options
  • Save levochkaa/b9c0ea327f225b2fbd26f623288addf3 to your computer and use it in GitHub Desktop.
Save levochkaa/b9c0ea327f225b2fbd26f623288addf3 to your computer and use it in GitHub Desktop.
iOS 17, ScrollView, .scrollTargetLayout() modifier example
import SwiftUI
struct ContentView: View {
@State var data: [String] = (0 ..< 25).map { String($0) }
@State var dataID: String?
var dataIDText: String {
dataID.map(String.init(describing:)) ?? "None"
}
var body: some View {
ScrollView {
VStack {
Text("Header")
LazyVStack {
ForEach(data, id: \.self) { item in
Color.red
.frame(width: 100, height: 100)
.overlay {
Text("\(item)")
.padding()
.background()
}
}
}
.scrollTargetLayout()
}
}
.scrollPosition(id: $dataID)
.safeAreaInset(edge: .bottom) {
Menu {
Button("Prepend") {
let next = String(data.count)
data.insert(next, at: 0)
}
Button("Append") {
let next = String(data.count)
data.append(next)
}
Button("Remove First") {
data.removeFirst()
}
Button("Remove Last") {
data.removeLast()
}
} label: {
Label("More", systemImage: "ellipsis.circle")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment