Skip to content

Commit

Permalink
Scroll Down The Chats Screen When User Starts Writing a Message or Se…
Browse files Browse the repository at this point in the history
…nds a Message(No Scroll Down While Writing a Message)
  • Loading branch information
sanghee-dev committed Aug 6, 2021
1 parent 68c54e0 commit 9acbc0f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions Hello Chat/Utilities/Extensions.swift
Expand Up @@ -35,3 +35,4 @@ extension String {
return false
}
}

23 changes: 19 additions & 4 deletions Hello Chat/View/Messages/ChatsView.swift
Expand Up @@ -12,18 +12,33 @@ struct ChatsView: View {
@ObservedObject var viewModel: ChatsViewModel
private let chatPartner: User

@Namespace var topID
@Namespace var bottomID

init(chatPartner: User) {
self.chatPartner = chatPartner
self.viewModel = ChatsViewModel(chatPartner: chatPartner)
}

var body: some View {
VStack {
ScrollView {
VStack(spacing: 16) {
ForEach(viewModel.messages) { message in
MessageView(viewModel: MessageViewModel(message))
ScrollViewReader { proxy in
ScrollView {
Button(
action: { withAnimation { proxy.scrollTo(bottomID) } },
label: { Image(systemName: "chevron.down").foregroundColor(Color(.systemGray2)) })
.id(topID)

VStack(spacing: 16) {
ForEach(viewModel.messages) { message in
MessageView(viewModel: MessageViewModel(message))
}
}
.onChange(of: messageText == "") { _ in
withAnimation { proxy.scrollTo(bottomID) }
}

Text("").id(bottomID)
}
}

Expand Down

0 comments on commit 9acbc0f

Please sign in to comment.