43

On Android there's neat way to browse between several pages of data with the same layout by using ViewPager and Fragments. User swipes left or right, and the pages change. There's only one view controller and one layout resource for all pages, but the data content changes when the user browses. There's also an animation effect to the swiping left and right where you can see the content of the next page is already loaded.

What is the equivalent to this on iOS?

Vanja
  • 4,415
  • 3
  • 26
  • 22
  • I am looking for the exact same thing. I am porting an android app to iOS and am not sure whether to use ScrollView, a Horizontal TableView, the PageViewController, etc. Also I am not sure how to handle the dynamic allocation or freeing of the views which were automatic in Android since each detail view was its own fragment which was freed when no longer needed by the ViewPager and Adapter etc. What is the equivalent? – Mike Kogan Mar 13 '14 at 02:25
  • 1
    Using Container view and UIPageViewController we can do this.. https://stackoverflow.com/a/45188331/4665694 – Nagaraju L J Jul 19 '17 at 11:26

5 Answers5

47

Use a UIScrollView with the pagingEnabled property set to YES. Typically a UIPageControl is used along with it.

If you Google for "UIScrollView paging", you can find many tutorials and examples. Apple provides a PageControl sample program.

iOS doesn't provide any sort of adapter-like class, but here is a tutorial that explains how to implement "virtual pages" so that you don't have to instantiate all the pages at once: http://cocoawithlove.com/2009/01/multiple-virtual-pages-in-uiscrollview.html

Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302
27

You should look at UIPageViewController. Its available since iOS 5.

Ingolmo
  • 575
  • 3
  • 12
  • Thank you, but it seem like this is navigation _between_ view controllers instead of using an adapter of data to one and the same view controller. I have an unspecified amount of "pages", their content is downloaded from a web serice. – Vanja Mar 16 '12 at 09:56
  • 1
    You could create one ViewController class with several objects of them for each page, and add it to the PageViewController. – Ingolmo Mar 16 '12 at 09:59
  • That's true. That's probably the way to do it on iOS. I will have to support at least iOS 4 however, so I'll try to see if I can use the UISwipeGestureRecognizer. Thanks :) – Vanja Mar 16 '12 at 10:18
  • Yup, I don't think there is an easy way to do it on iOS 4. (But you can UpVote me if I helped you :P) – Ingolmo Mar 16 '12 at 10:28
7

I've created a custom control for iOS which acts similar to android's viewPager. You can check this out here.

Shabib
  • 1,697
  • 4
  • 20
  • 39
0

visit https://github.com/vowed21/HWViewPager

It may help you.

Inherited UICollectionView.

Reuseable Cell, (likely Android's Adapter)

AND enable Preview part of left, right side.

hyunwoo
  • 11
  • HI There i'm also using HWView pager as a sub view in an application from super view when i call this HWViewpager reload data its reloading the whole view data. Plz help if you have some idea about it. – Waqar Ahmed Jan 13 '17 at 07:45
0

You can implement it your own using following idea.

Suppose that we wish to infinitely loop through 3 items(cells) - C0, C1, C2, we can generate dummy cells at the left and right side of center cells, the result as follows,

C0 C1 C2 [C0 C1 C2] C0 C1 C2

Cells in bracket are cells we see through device screen, and if we scroll to left,

C0 [C1 C2 C0] C1 C2 C0 C1 C2

at this moment, force contentOffset to point right side of given dummy cells,

C0 [C1 C2 C0] C1 C2 C0 C1 C2 -> C0 C1 C2 C0 [C1 C2 C0] C1 C2

It will works in same mechanism when you scroll it to right.

In my solution below, setting item view width to equal to its parent view will solve your problem.

https://github.com/DragonCherry/HFSwipeView

If you just wanna simply check how it works, click link below and "tap to play".

https://www.cocoacontrols.com/controls/hfswipeview

Just using pagingEnabled option in UIScrollView may work in case of full-sized cell item, but it will not work properly if you want to set narrower width of content item than its parent(scroll) view. Please check this feature by referring to cocoacontrols sample for "Sync" or "Edge Preview".