【问题标题】:UITableView - How to check whether a section footer is visible completely on screen and scroll accordingly to make section footer visible completely?UITableView - 如何检查部分页脚是否在屏幕上完全可见并相应滚动以使部分页脚完全可见?
【发布时间】:2021-11-24 05:30:06
【问题描述】:

UICollectionView有多个部分,我可以

  1. 检查节页脚是否在屏幕上完全可见。
  2. 如果没有,我将执行滚动,以使页脚完全可见。

这是我实现此目的的代码 sn-p。

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 中实现类似的行为?因为,我在UITableView 中找不到等效的collectionView.layoutAttributesForSupplementaryElement

【问题讨论】:

    标签: ios swift uitableview uicollectionview


    【解决方案1】:

    你可以使用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.
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      • 2017-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多