ScrollView Scroll To

Below you will find a little code snippet to scroll a ScrollView to a defined position.

ScrollViewReader { reader in
	ScrollView {
		ForEach(messages) { message in
			MessageBubble(message: message.message, isFromCurrentUser: message.fromUser)
			.id(message.id)
		}
	}
	.onChange(of: messages.count) {
		withAnimation {
			reader.scrollTo(messages.last?.id, anchor: .top)
		}
	}
	.padding()
}