Created
May 7, 2018 18:36
-
-
Save glm4/acae233f9ecd58c97740935af9586707 to your computer and use it in GitHub Desktop.
Swift UIScrollView focus on specific subview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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