在UICollectionView多個部分中,我可以
- 檢查節頁腳是否在螢屏上完全可見。
- 如果沒有,我將執行滾動,以使頁腳完全可見。
這是我的代碼片段來實作這一點。
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)
}
}
}
我怎樣才能實作類似的行為UITableView?因為,我collectionView.layoutAttributesForSupplementaryElement在UITableView.
uj5u.com熱心網友回復:
您可以使用 rectForFooter 方法
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.
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/326563.html
標籤:ios 迅速 合适的视图 uicollectionview
