UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

TabView as horizontal paged list. Two issues

Forums > SwiftUI

I use a TabView to simulate a horizontal scrolling paged list. This work fine. But I want to change the background, according to the currently active tab. I utilise .onAppear() for this, but this results in the images not being in sync with the item in the current tab (see video: https://youtube.com/shorts/aQxrErRmCi8?feature=share)

basically, it should only change the background whenever the tab is 'fully' selected and centered on screen. Not as soon as the next or previous tab comes onto screen just a few pixels.

Secondly, I want to be able to set the selected programmatically, for instance after I insert a new object, I want to select the first tab. How Can I achieve that. The tabs have the id of my item (game) as tag.

Relevant code:

    @ViewBuilder var tabList: some View {
        TabView {
            ForEach(gamesViewModel.games) { game in
                GameCardView(game: game)
                    .tag(game.id)
                    .onAppear {
                        withAnimation(.easeInOut) {
                            backgroundImage = gamesViewModel.getGameImageCoreData(for: game)
                        }
                    }
                    .listRowBackground(Color.clear)
                    .frame(width: getScreenRectangle().width - 20)
                    .onTapGesture { showAnnotationsView(for: game) }
                    .onLongPressGesture(minimumDuration: 0.15) { gameDetailsView(for: game) }
            }
        }
        .tabViewStyle(.page(indexDisplayMode: .never))
    }

1      

anyone?

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.