A lot of answers include setting the contentOffset
to 0. I had a case in which I wanted the view inside the scrollView to be centered. This did the job for me:
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
scrollView.contentOffset.y = -scrollView.contentInset.top
}