Skip to content

Instantly share code, notes, and snippets.

@cscouto
Last active February 17, 2018 03:01
Show Gist options
  • Save cscouto/a62df599008b2326661497fbbd4d1265 to your computer and use it in GitHub Desktop.
Save cscouto/a62df599008b2326661497fbbd4d1265 to your computer and use it in GitHub Desktop.
SWIFT - Scrolling to bottom, top and to any view
import UIKit
extension UIScrollView {
func scrollToView(view:UIView, animated: Bool, navigationHeight: CGFloat?) {
let childStartPoint = view.frame.origin
let height = (navigationHeight ?? 0) * 2 + 16
self.scrollRectToVisible(
CGRect(x: 0,
y: childStartPoint.y - height,
width: 1,
height: self.frame.height),
animated: animated)
}
func scrollToTop(animated: Bool) {
let topOffset = CGPoint(x: 0, y: -contentInset.top)
setContentOffset(topOffset, animated: animated)
}
func scrollToBottom() {
let bottomOffset = CGPoint(x: 0,
y: contentSize.height - bounds.size.height + contentInset.bottom)
if(bottomOffset.y > 0) {
setContentOffset(bottomOffset, animated: true)
}
}
}
DispatchQueue.main.async {
let indexPath = IndexPath(row: self.list.count-1, section: 0)
self.tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.bottom, animated: true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment