Should I rotate the scrollView to start it from bottom? SwiftUI

74 Views Asked by At

I am creating a chatScreen which should start from the bottom and automatically scrolls to last message every time a new message arrives, I saw people implementing different types of methods to implement this like auto scroll to last message when count increases in messageList and scroll to last message in onAppear(), but every time I opened chatScreen, I saw scrollView scrolling to bottom.

I tried to rotate scrollView, messageText 180 degree and reverse the messageList, this solved "auto scrolling to bottom" and "start from last message bottom" problems.

var body: some View {
    VStack {
        ScrollView(.vertical, showsIndicators: true, content:{
            ForEach(chatViewModel.messages.reversed(), id: \.id) { message in
                MessageView(message: message, userId: userId).rotationEffect(Angle(degrees: 180)).scaleEffect(x: -1.0, y: 1.0, anchor: .center)
            }
            .padding()
        })
        .background(.opacity(0.05))
        .frame(maxWidth: .infinity)
        .rotationEffect(Angle(degrees: 180)).scaleEffect(x: -1.0, y: 1.0, anchor: .center)
        .scrollDismissesKeyboard(.interactively)
        .navigationTitle(groupName)
        .navigationBarTitleDisplayMode(.inline)
        HStack {
            TextField("Type your message here", text: $message)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .controlSize(.large)
            Button {
                chatViewModel.sendMessage(messageText: message, userId: userId, groupId: groupId, userName: userName ?? "")
                message = ""
            } label: {
                Image(systemName: "arrow.up.circle.fill")
                    .foregroundStyle(.blue)
                    .font(.system(size: 30))
            }
        }
        .padding(.horizontal)
        .padding(.bottom, 7)
        .padding(.top, 1)
    }
}

My question is, is this approach bad in the long run? because I do not see anyone else doing this.

0

There are 0 best solutions below