Centering the content view inside a scroll view has always taken a considerable amount of manual work. You would think it would be simple to have the content view centered as you zoom out of a scroll view, but this has not been the case.
In the WWDC 2017 “What’s New in Cocoa Touch” session, an Apple engineer mentions they had spent 1/3rd of a previous year’s session demonstrating how to implement this effect.
iOS 11 changes that.
Here is all the code you need going forward:
contentView.centerXAnchor.constraint(equalTo: scrollView.contentLayoutGuide.centerXAnchor) // Do the same for Y axis contentView.centerYAnchor.constraint(equalTo: scrollView.contentLayoutGuide.centerYAnchor)
And that is it!
I love when Apple takes simple features that required several lines of code to implement, and reduces them to just a couple lines of code. Hope everyone enjoys this simplified approach! 🙂