1

I see we can scroll to any item in ScrollView thanks to the ScrollViewReader feature of iOS 14 and Xcode 12 but I want the same thing on iOS 13 and Xcode 11.

I have a message page with contacts at the top and messages list below it. I want when the user selects a contact, scrollView must scroll to that selected item. When I select any contact that not shown screen (for example: last one), scrollView turning back to the first item. I don't want that behavior.

You can understand what I mean thanks to the screenshot that below.

Screenshot from messages page

ScrollView:

ScrollView(.horizontal, showsIndicators: false) {
                HStack(spacing: 15){
                    ForEach(self.viewModel.contacts.indices, id: \.self){ index in
                        Button(action: {
                            self.selectedUser = index
                            self.viewModel.selectedUser = self.viewModel.contacts[index].user!.id
                            self.viewModel.loadMoreMessages()
                        }) {
                                ZStack(alignment: .topLeading) {
                                    KFImage(URL(string: self.viewModel.contacts[index].user!.avatar!.userAvatarPathSmall()))
                                        .resizable()
                                        .aspectRatio(contentMode: .fill)
                                        .animation(.easeInOut(duration: 0.5))
                                        .frame(width: 52, height: 52)
                                        .cornerRadius(26)
                                    if self.viewModel.contacts[index].unread! > 0 {
                                        ZStack {
                                            Circle()
                                                .foregroundColor(.red)
                                            Text("\(self.viewModel.contacts[index].unread!)")
                                                .foregroundColor(.white)
                                                .font(Font.system(size: 12))
                                        }.frame(width: 20, height: 20)
                                    } else {
                                        EmptyView()
                                    }
                                }.contextMenu {
                                    Button(action: {
                                    }) {
                                        Text(self.viewModel.contacts[index].user!.name!)
                                    }
                                    
                                    Button(action: {
                                    }) {
                                        Text("@\(self.viewModel.contacts[index].user!.username!)")
                                    }
                                }
                        }.buttonStyle(PlainButtonStyle())
                    }
                }.padding(.horizontal, 20)
            }.id(UUID().uuidString).frame(height: 76)

0 Answers0