-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathScrollFromBottom.swift
47 lines (41 loc) · 1.39 KB
/
ScrollFromBottom.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// ContentView.swift
// ScrollFromBottom
//
// Created by Ganesh on 02/08/23.
//
import SwiftUI
struct ScrollFromBottom: View {
@State private var items: [Int] = Array(0..<50)
var body: some View {
NavigationView {
ScrollView {
ScrollViewReader { proxy in
VStack(spacing: 10) {
ForEach(items, id: \.self) { index in
RoundedRectangle(cornerRadius: 10)
.frame(maxWidth: .infinity)
.frame(height: 100)
.foregroundColor(Color(red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1)))
.id(index)
}
}
.padding(12)
.onChange(of: items.count) { _ in
DispatchQueue.main.async {
withAnimation {
proxy.scrollTo(items.count - 1, anchor: .bottom)
}
}
}
}
}
.navigationBarItems(trailing:
Button("Add Item") {
items.append(items.count)
}
)
.navigationBarTitle(Text("ScrollFromBottom"))
}
}
}