2

I have a ForEach loop inside a TabView.

struct PageView: View {
    var imageViewModels: [ImageViewModel]
    var height: CGFloat
    var body: some View {
        
        TabView {
            ForEach(imageViewModels) { viewModel in
                Image(viewModel.name)
            }
        }
        .frame(width: UIScreen.main.bounds.width, height: height)
        .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
        
    }
}

My question: Is there a way to loop the images inside the tab view? Ex: If the user swipes right on the first image he goes to the last one. If he swipes left on the last image he goes to the first one.

Rubens Neto
  • 802
  • 1
  • 6
  • 8
  • It looks like you need UIPageViewController for this. See this answer: [How can I implement PageView in SwiftUI?](https://stackoverflow.com/a/58396319/8697793) – pawello2222 Oct 01 '20 at 21:30
  • Thank you for your comment @pawello2222 . But the PageTabViewStyle does exactly the same thing as a UIPageViewController as far as I'm aware. My problem is that I can't put it in a loop like a carousel. Do you think I'd be able to do that with a UIPageViewController? – Rubens Neto Oct 02 '20 at 07:36
  • 1
    UIPageViewController allows for much more customisation. In the linked example see how the `func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController)` function (for changing the next tab) is overriden. – pawello2222 Oct 02 '20 at 08:20
  • 1
    And especially the part: `if index + 1 == parent.controllers.count { return parent.controllers.first }` – pawello2222 Oct 02 '20 at 08:21

0 Answers0