In UICollectionView
with multiple sections, I can
- Check whether a section footer is visible completely on screen.
- If it doesn't, I will perform scrolling, to make the footer visible completely.
Here's my code snippet to achieve so.
private func ensureSectionFooterIsVisibleIfPossible(_ section: Int) {
//
// Ensure footer is visible.
// https://stackoverflow.com/questions/25201646/uicollectionview-scroll-to-any-footer-or-header-view/31250801
//
let indexPath = IndexPath(item: 0, section: section)
if let layoutAttributes = collectionView.layoutAttributesForSupplementaryElement(ofKind: UICollectionView.elementKindSectionFooter, at: indexPath) {
var visibleRect: CGRect = CGRect()
visibleRect.origin = collectionView.contentOffset
visibleRect.size = collectionView.bounds.size
let footerRect = layoutAttributes.frame
//
// Is this section footer visible completely on screen?
//
if !visibleRect.contains(footerRect) {
//
// If not, scroll till the section footer visible completely.
//
var resultOffset : CGPoint = collectionView.contentOffset
resultOffset.y = (footerRect.origin.y footerRect.size.height) - (collectionView.contentInset.top collectionView.frame.size.height)
collectionView.scrollRectToVisible(CGRect(origin: resultOffset, size: collectionView.frame.size), animated: true)
}
}
}
How can I achieve the similar behavior in UITableView
? As, I do not find an equivalent collectionView.layoutAttributesForSupplementaryElement
in UITableView
.
CodePudding user response:
You can use rectForFooter method
var visibleRect: CGRect = CGRect()
visibleRect.origin = tableview.contentOffset
visibleRect.size = tableview.bounds.size
let footerRect = tableview.rectForFooter(inSection: 0)
// Is this section footer visible completely on screen?
if !visibleRect.contains(footerRect) {
// If not, scroll till the section footer visible completely.
}