Last active
February 17, 2018 03:01
-
-
Save cscouto/a62df599008b2326661497fbbd4d1265 to your computer and use it in GitHub Desktop.
SWIFT - Scrolling to bottom, top and to any view
This file contains 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
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