Kodeco Forums

Video Tutorial: Swift Scroll View School Part 10: Paging Scroll Views I

In this video tutorial you'll learn about paging scroll views and how to center a page when the user stops scrolling.


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

For iOS 9, need to change:

tutorialStep.view.setTranslatesAutoresizingMaskIntoConstraints(false)

to

tutorialStep.view.translatesAutoresizingMaskIntoConstraints = false

Does anyone know what to put for the verticalConstraints since in iOS 9, the options can not be nil.

Does anyone know what to put for the verticalConstraints since in iOS 9, the options can not be nil.

let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|[page1(==view)]|", options: [], metrics: nil, views: views)

Hi @jbireton,

You can use : NSLayoutFormatOptions.init(rawValue = 0)

see if that works.

Hi, The code doesn’t work for any started kit because xcode is forcing me to use(convert swift2) swift 3.Can you please help?

Hi. Here is the code in swift 3:

let verticalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:|[page1(==view)]|", options: .init(rawValue: 0), metrics: nil, views: views)
NSLayoutConstraint.activate(verticalConstraints)

let horizontalConstraints =
    
    NSLayoutConstraint.constraints(withVisualFormat: "H:|[page1(==view)][page2(==view)][page3(==view)][page4(==view)][page5(==view)]|", options: [.alignAllTop, .alignAllBottom], metrics: nil, views: views)

NSLayoutConstraint.activate(horizontalConstraints)

The code for keyboard insets works as expected but when i tap on other textfield it keeps on scrolling to the top. I tried using contains method of CGRect but it doesn’t help. How can i control this ?

There are a few issues with this example:

First off adding +20 for both hide and show screws up the adjustment value. The -/+ value for the 20 should also be accounted for based on the show value.

let adjustment = ((keyboardFrame.height + 20) * (show ? 1 : -1))

Secondly, keyboardWillShow gets called twice for some reason.

Thirdly, The keyboardFrame height decreases by 45 on the second and subsequent calls.
:expressionless:

Can you explain the difference between contentInset and scrollIndcatorInset a bit more? I commented out the scrollIndcatorInset and the results seem to be the same. @bdmoakley

@bdmoakley Also, in the challenge when tapping on the other textfields (while the keyboard is still up), the scroll view keeps scrolling up. Is this a bug? It seems that the keyboardWillShow(:) method is called each time you select a different textfield even if the keyboard is already up.