Skip to content

Instantly share code, notes, and snippets.

@glm4
Created May 7, 2018 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glm4/acae233f9ecd58c97740935af9586707 to your computer and use it in GitHub Desktop.
Save glm4/acae233f9ecd58c97740935af9586707 to your computer and use it in GitHub Desktop.
Swift UIScrollView focus on specific subview
extension UIScrollView {
func scroll(toSubview view: UIView, animated: Bool = true) {
guard view.isDescendant(of: self) else { return }
let visibleRect = CGRect(origin: contentOffset,
size: frame.size)
let realRect = view.convert(view.frame, to: self
//View is out of scroll visible area
if !visibleRect.contains(realRect) || realRect.maxY <= 0 {
let yPos = realRect.maxY - visibleRect.height
setContentOffset(CGPoint(x: 0, y: yPos), animated: animated)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment