Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

Video Tutorial: Swift Scroll View School Part 12: UIPageViewController

In this video tutorial you'll learn how to use UIPageViewControllers to create paging scroll views with increased efficiency.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3911-scroll-view-school/lessons/13

What if the pages are different heights? Could you give some direction on how to handle this situation? And, by different heights, I mean they scroll vertically, off the page, in addition to paging horizontally.

Thanks!

Are there Swift 3 files for these lessons? I downloaded Lesson 12 & tried to do the automatic conversion, but some items aren’t converting.
Thanks!
J

I am struggling to implement this on Swift 3 as well…

I was able to get a Swift 3 version working & uploaded it to GitHub at:
https://github.com/gallaugher/Swift3ScrollSchoolPart13

One question: in PageTutorialViewController.swift, the tutorialSetForPage code below:

func tutorialStepForPage(_ inPage: Int) -> TutorialStepViewController {

let tutorialStep = storyboard!.instantiateViewController(withIdentifier: "TutorialStepViewController") as! TutorialStepViewController
let page = min(max(0, inPage), pageCount-1)
tutorialStep.page = page

why do we need:
let page = min(max(0, inPage), pageCount-1)

Doesn’t page = inPage do the same thing? I’m struggling to figure out the reason for the min(max.
Thanks,
John

Hi @gallaugher, thanks for getting a Swift 3 version up and running!

The min/max calls are to clamp the value and make sure the page argument is within the bounds. So max(0, page) ensures the value will not be a negative number (less than 0) and min(x, pageCount-1) ensures the value will not be greater than the index of the final page.

If you pass in a negative number mistakenly for inPage it will be set to 0; if you pass in a really large number beyond the actual number of pages, the value will get set to pageCount-1.

In short, it’s a safety feature to ensure we don’t go out of bounds.

I hope that helps, and thanks for the question and the code!
–Greg

Super - thanks. I tried removing this & noticed that there were situations where my code crashed, perhaps because events are firing in ways that are unexpected, so having the extra check is useful. Thanks again for the tutorial!
J

First of all, thank you for putting up these scrollview lessons, they’ve been extremely informative and helpful. I am trying implement a scrollview with peeking views (like what the safari app used to have for switching between tabs). I’m trying to implement with autolayout but cannot get the view to center properly when there are left and right views to either side. I pinned the scrollview edges so that the scrollview is narrower than the superview, and tried modifying the padding, but am not able to get the current view centered properly.