79222318

Date: 2024-11-25 09:07:45
Score: 1.5
Natty:
Report link

As I understand it, this example may be helpful. Please give it a try.

struct ContentView: View {
    
    let messages = Message.dummyData
    @State private var tappedMessage: Message?
    @State private var newMessage = ""
    @FocusState private var focus: Bool

    var body: some View {
        
        ScrollViewReader { scrollReader in
            ScrollView {
                LazyVStack(alignment: .leading, spacing: 24) {
                    ForEach(messages, id: \.id) { message in
                        MessageContainer(message: message)
                            .id(message.id)
                            .onTapGesture {
                                tappedMessage = message
                                focus = true
                            }
                    }
                }
                .padding(.horizontal, 16)
            }
            if let tappedMessage {
                VStack {
                    TextEditor(text: $newMessage)
                        .frame(height: 80)
                        .padding()
                        .background(
                            RoundedRectangle(cornerRadius: 20)
                                .stroke(Color.gray, lineWidth: 1)
                        )
                        .padding(.horizontal, 16)
                        .focused($focus)
                    
                    Button("Send") { self.tappedMessage = nil }
                }
                .onAppear {
                    DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
                        withAnimation {
                            scrollReader.scrollTo(tappedMessage.id)
                        }
                    }
                }
           }
        }
    }
}
Reasons:
  • RegEx Blacklisted phrase (2.5): Please give
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Muhammad Barznji