Thursday, October 8, 2026
HomeiOS Developmentios - How you can know if the primary merchandise within the...

ios – How you can know if the primary merchandise within the listing that’s inside a ForEach loop is seen in SwiftUI?

[ad_1]

You need to use enumerated() together with ForEach to trace an index of the array and use it to cover and present your button. One thing like this.

struct ContentView: View {
    
    let gadgets = (1...50).map(String.init)
    @State non-public var showScrollButton = false
   
    var physique: some View {
        ScrollViewReader { reader in
            ZStack(alignment: .bottomTrailing) {
                Record {
                    let enumerated = gadgets.enumerated().map({ $0 })
                    ForEach(enumerated, id: .factor) { (index, merchandise) in
                        Textual content(merchandise)
                            .id(index)
                            .onAppear {
                                guard index == 0, showScrollButton else {
                                    return
                                }
                                showScrollButton = false
                            }
                            .onDisappear {
                                guard index == 0, !showScrollButton else {
                                    return
                                }
                                showScrollButton = true
                            }
                    }
                }
                if showScrollButton {
                    Button(motion: {
                        withAnimation {
                            reader.scrollTo(0, anchor: .prime)
                        }
                    }, label: {
                        Picture(systemName: "chevron.up.circle")
                            .font(.largeTitle)
                            .foregroundColor(.black)
                            .padding()
                            .padding(.trailing)
                    })
                }
            }
        }
    }
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments