Skip to content

Instantly share code, notes, and snippets.

@DanielCardonaRojas
Created October 24, 2018 14:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielCardonaRojas/4e69706b61b56000f11e9fa84666705a to your computer and use it in GitHub Desktop.
Save DanielCardonaRojas/4e69706b61b56000f11e9fa84666705a to your computer and use it in GitHub Desktop.
UIScrollView Extensions (scroll to subview and get relative offset of subview)
extension UIScrollView {
func scrollSubViewToTop(_ subview: UIView, offset: CGFloat, animated: Bool) {
let point = convert(subview.frame.origin, from: subview.superview ?? subview)
setContentOffset(CGPoint(x: 0, y: point.y - offset), animated: animated)
}
func viewPortOffset(of subview: UIView) -> CGFloat {
let point = convert(subview.frame.origin, from: subview.superview ?? subview)
return point.y - contentOffset.y
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment