2014-12-05 4 views
12

Я пытаюсь создать липкий дополнительный заголовок, который все время остается в верхней части и не будет отвечать на события прокрутки. Решения, которые я нашел до сих пор, все еще реагируют на прокрутку и исправляются с использованием пользовательского потока, что, вероятно, также будет проблемой для моей проблемы.UICollectionView липкий заголовок в swift

Причина, по которой я хочу этого, заключается в том, что заголовок используется в других местах и ​​должен быть повторно использован. Я надеюсь, что это можно решить таким образом, и мне не нужно создавать отдельный вид.

Как я делаю это в Свифт, было бы здорово иметь пример в Свифт.

ответ

5

Окончательное решение я нашел:

Используя этот макет пользовательского потока можно было зафиксировать этот липкий заголовок:

class StickyHeaderCollectionViewFlowLayout: UICollectionViewFlowLayout { 

    override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? { 

     var superAttributes: [UICollectionViewLayoutAttributes]? = super.layoutAttributesForElementsInRect(rect) as? [UICollectionViewLayoutAttributes] 

     if superAttributes == nil { 
      // If superAttributes couldn't cast, return 
      return super.layoutAttributesForElementsInRect(rect) 
     } 

     let contentOffset = collectionView!.contentOffset 
     var missingSections = NSMutableIndexSet() 

     for layoutAttributes in superAttributes! { 
      if (layoutAttributes.representedElementCategory == .Cell) { 
       if let indexPath = layoutAttributes.indexPath { 
        missingSections.addIndex(layoutAttributes.indexPath.section) 
       } 
      } 
     } 

     for layoutAttributes in superAttributes! { 
      if let representedElementKind = layoutAttributes.representedElementKind { 
       if representedElementKind == UICollectionElementKindSectionHeader { 
        if let indexPath = layoutAttributes.indexPath { 
         missingSections.removeIndex(indexPath.section) 
        } 
       } 
      } 
     } 

     missingSections.enumerateIndexesUsingBlock { idx, stop in 
      let indexPath = NSIndexPath(forItem: 0, inSection: idx) 
      if let layoutAttributes = self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: indexPath) { 
       superAttributes!.append(layoutAttributes) 
      } 
     } 

     for layoutAttributes in superAttributes! { 
      if let representedElementKind = layoutAttributes.representedElementKind { 
       if representedElementKind == UICollectionElementKindSectionHeader { 
        let section = layoutAttributes.indexPath!.section 
        let numberOfItemsInSection = collectionView!.numberOfItemsInSection(section) 

        let firstCellIndexPath = NSIndexPath(forItem: 0, inSection: section)! 
        let lastCellIndexPath = NSIndexPath(forItem: max(0, (numberOfItemsInSection - 1)), inSection: section)! 


        let (firstCellAttributes: UICollectionViewLayoutAttributes, lastCellAttributes: UICollectionViewLayoutAttributes) = { 
         if (self.collectionView!.numberOfItemsInSection(section) > 0) { 
          return (
           self.layoutAttributesForItemAtIndexPath(firstCellIndexPath), 
           self.layoutAttributesForItemAtIndexPath(lastCellIndexPath)) 
         } else { 
          return (
           self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: firstCellIndexPath), 
           self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionFooter, atIndexPath: lastCellIndexPath)) 
         } 
         }() 

        let headerHeight = CGRectGetHeight(layoutAttributes.frame) 
        var origin = layoutAttributes.frame.origin 

        origin.y = min(contentOffset.y, (CGRectGetMaxY(lastCellAttributes.frame) - headerHeight)) 
        // Uncomment this line for normal behaviour: 
        // origin.y = min(max(contentOffset.y, (CGRectGetMinY(firstCellAttributes.frame) - headerHeight)), (CGRectGetMaxY(lastCellAttributes.frame) - headerHeight)) 

        layoutAttributes.zIndex = 1024 
        layoutAttributes.frame = CGRect(origin: origin, size: layoutAttributes.frame.size) 
       } 
      } 
     } 

     return superAttributes 
    } 

    override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool { 
     return true 
    } 

} 

Для создания макета, где заголовки являются липкие, как традиционные, изменения эта линия:

origin.y = min(contentOffset.y, (CGRectGetMaxY(lastCellAttrs.frame) - headerHeight)) 

этой линии:

origin.y = min(max(contentOffset.y, (CGRectGetMinY(firstCellAttrs.frame) - headerHeight)), (CGRectGetMaxY(lastCellAttrs.frame) - headerHeight)) 

Надеясь, что это полезно для других!

Update

Обновлено исправить аварии (спасибо Роберт Аткинс!) И некоторые обновления Swift 1,2

tvOS & IOS 9

tvOS и IOS 9 введены свойство sectionHeadersPinToVisibleBounds, который может быть использован

+0

Я получаю EXC_ARITHMETIC аварии на линии 'firstCellAttrs = ...', возможно, что-то связано с http://stackoverflow.com/questions/24616797/uicollectionviewflowlayout-subclass-crashes-accessing-array- за пределами границ? –

+0

По-видимому, это не работает для меня: Xcode 6.1.1 и iOS 8 :-(Любые идеи? Могли бы вы провести повторный тест? Заголовок должен придерживаться вершины, как в https://github.com/jamztang/CSStickyHeaderFlowLayout right ? – blackjacx

+0

Не работает для меня сбои здесь 'self.layoutAttributesForSupplementaryViewOfKind (UICollectionElementKindSectionFooter, atIndexPath: lastCellIndexPath))' –

2

Я исправил свою ошибку, проверив тестирование для пустых разделов как предложено в this gist. Я также добавил пару if let s для дополнительных очков стиля Swift ;-). Это теперь работает для меня:

class StickyHeaderCollectionViewFlowLayout: UICollectionViewFlowLayout { 

    override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? { 

     var answer: [UICollectionViewLayoutAttributes] = super.layoutAttributesForElementsInRect(rect)! as [UICollectionViewLayoutAttributes] 
     let contentOffset = collectionView!.contentOffset 

     var missingSections = NSMutableIndexSet() 

     for layoutAttributes in answer { 
      if (layoutAttributes.representedElementCategory == .Cell) { 
       if let indexPath = layoutAttributes.indexPath { 
        missingSections.addIndex(layoutAttributes.indexPath.section) 
       } 
      } 
     } 

     for layoutAttributes in answer { 
      if let representedElementKind = layoutAttributes.representedElementKind { 
       if representedElementKind == UICollectionElementKindSectionHeader { 
        if let indexPath = layoutAttributes.indexPath { 
         missingSections.removeIndex(indexPath.section) 
        } 
       } 
      } 
     } 

     missingSections.enumerateIndexesUsingBlock { idx, stop in 
      let indexPath = NSIndexPath(forItem: 0, inSection: idx) 
      if let layoutAttributes = self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: indexPath) { 
       answer.append(layoutAttributes) 
      } 
     } 

     for layoutAttributes in answer { 
      if let representedElementKind = layoutAttributes.representedElementKind { 
       if representedElementKind == UICollectionElementKindSectionHeader { 
        let section = layoutAttributes.indexPath!.section 
        let numberOfItemsInSection = collectionView!.numberOfItemsInSection(section) 

        let firstCellIndexPath = NSIndexPath(forItem: 0, inSection: section)! 
        let lastCellIndexPath = NSIndexPath(forItem: max(0, (numberOfItemsInSection - 1)), inSection: section)! 


        let (firstCellAttributes: UICollectionViewLayoutAttributes, lastCellAttributes: UICollectionViewLayoutAttributes) = { 
         if (self.collectionView!.numberOfItemsInSection(section) > 0) { 
          return (
           self.layoutAttributesForItemAtIndexPath(firstCellIndexPath), 
           self.layoutAttributesForItemAtIndexPath(lastCellIndexPath)) 
         } else { 
          return (
           self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: firstCellIndexPath), 
           self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionFooter, atIndexPath: lastCellIndexPath)) 
         } 
        }() 

        let headerHeight = CGRectGetHeight(layoutAttributes.frame) 
        var origin = layoutAttributes.frame.origin 

        origin.y = min(max(contentOffset.y, (CGRectGetMinY(firstCellAttributes.frame) - headerHeight)), (CGRectGetMaxY(lastCellAttributes.frame) - headerHeight)) 

        layoutAttributes.zIndex = 1024 
        layoutAttributes.frame = CGRect(origin: origin, size: layoutAttributes.frame.size) 
       } 
      } 
     } 

     return answer 
    } 

    override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool { 
     return true 
    } 

} 
+1

Спасибо за это! Я обновил свой ответ с обновленной частью кода. – Antoine

+0

Спасибо! FYI, в Swift 1.2 первая строка layoutAttributesForElementsIntRect теперь требует «как!» var ответ: [UICollectionViewLayoutAttributes] = super.layoutAttributesForElementsInRect (rect)! в виде! [UICollectionViewLayoutAttributes] –

+0

У меня есть «конфликт определений с предыдущим значением» в 'let (firstCellAttributes: UICollectionViewLayoutAttributes, lastCellAttributes: UICollectionViewLayoutAttributes) = {' в swift 2.0 – Liumx31

-2

Я использую это в своем проекте. Надеюсь, поможет.

func collectionView(collectionView: UICollectionView, 
    layout collectionViewLayout: UICollectionViewLayout, 
    referenceSizeForHeaderInSection section: Int) -> CGSize 
{ 
    return CGSizeMake(UIScreen.mainScreen().bounds.width, 40) 
} 

конечно. вы можете вернуть нужный размер.

+0

Это только устанавливает высоту заголовка, но не создает липкий заголовок .. – Antoine

1

Это простое решение, которое работает, если у вас есть только один раздел.

class StickyHeaderLayout: UICollectionViewFlowLayout { 

    override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool { 
    return true 
    } 

    override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? { 
    var attributes = super.layoutAttributesForElementsInRect(rect)! as! [UICollectionViewLayoutAttributes] 

     let offset   = collectionView?.contentOffset 


     for attrs in attributes { 
     if attrs.representedElementKind == nil { 
      let indexPath  = NSIndexPath(forItem: 0, inSection: attrs.indexPath.section) 
      let layoutAttributes = self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: indexPath) 

      attributes.append(layoutAttributes) 
     } 
     } 

     for attrs in attributes { 
     if attrs.representedElementKind == nil { 
      continue 
     } 

     if attrs.representedElementKind == UICollectionElementKindSectionHeader { 

      var headerRect = attrs.frame 
      headerRect.size.height = headerHeight 
      headerRect.origin.y = offset!.y 
      attrs.frame = headerRect 
      attrs.zIndex = 1024 
      break 
     } 
     } 

    return attributes 
    } 
} 
3

работает для меня с быстрой 2,0

override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 

    var superAttributes:NSMutableArray = NSMutableArray(array: super.layoutAttributesForElementsInRect(rect)!) as NSMutableArray 

    let contentOffset = collectionView!.contentOffset 
    var missingSections = NSMutableIndexSet() 

    for layoutAttributes in superAttributes { 
     if (layoutAttributes.representedElementCategory == .Cell) { 
      if let indexPath = layoutAttributes.indexPath { 
       missingSections.addIndex(layoutAttributes.indexPath.section) 
      } 
     } 
    } 

    for layoutAttributes in superAttributes{ 
     if let representedElementKind = layoutAttributes.representedElementKind { 
      if representedElementKind == UICollectionElementKindSectionHeader { 
       if let indexPath = layoutAttributes.indexPath { 
        missingSections.removeIndex(indexPath.section) 
       } 
      } 
     } 
    } 

    missingSections.enumerateIndexesUsingBlock { idx, stop in 
     let indexPath = NSIndexPath(forItem: 0, inSection: idx) 
     let layoutAttributes = self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: indexPath) 
     superAttributes.addObject(layoutAttributes!) 
    } 

    for la in superAttributes { 

     let layoutAttributes = la as! UICollectionViewLayoutAttributes; 

     if let representedElementKind = layoutAttributes.representedElementKind { 
      if representedElementKind == UICollectionElementKindSectionHeader { 
       let section = layoutAttributes.indexPath.section 
       let numberOfItemsInSection = collectionView!.numberOfItemsInSection(section) 

       let firstCellIndexPath = NSIndexPath(forItem: 0, inSection: section) 
       let lastCellIndexPath = NSIndexPath(forItem: max(0, (numberOfItemsInSection - 1)), inSection: section)      

       var firstCellAttributes:UICollectionViewLayoutAttributes 
       var lastCellAttributes:UICollectionViewLayoutAttributes 

        if (self.collectionView!.numberOfItemsInSection(section) > 0) { 
          firstCellAttributes = self.layoutAttributesForItemAtIndexPath(firstCellIndexPath)! 
          lastCellAttributes = self.layoutAttributesForItemAtIndexPath(lastCellIndexPath)! 
        } else { 
          firstCellAttributes = self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: firstCellIndexPath)! 
          lastCellAttributes = self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionFooter, atIndexPath: lastCellIndexPath)! 
        } 

       let headerHeight = CGRectGetHeight(layoutAttributes.frame) 
       var origin = layoutAttributes.frame.origin 

       origin.y = min(max(contentOffset.y, (CGRectGetMinY(firstCellAttributes.frame) - headerHeight)), (CGRectGetMaxY(lastCellAttributes.frame) - headerHeight)) 
       ; 

       layoutAttributes.zIndex = 1024; 
       layoutAttributes.frame = CGRect(origin: origin, size: layoutAttributes.frame.size) 

      } 
     } 
    } 

    return NSArray(array: superAttributes) as? [UICollectionViewLayoutAttributes] 
} 

override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool { 
    return true 
} 
+0

этот код работает только для вертикальной прокрутки, вы можете заставить его работать для горизонтальной прокрутки? и заголовок секции сверху? – TomSawyer

1

очиститель Swift 2,3 протестирован вариант ответа Ирины

override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 

    guard var superAttributes = super.layoutAttributesForElementsInRect(rect) else { 
     return super.layoutAttributesForElementsInRect(rect) 
    } 

    let contentOffset = collectionView!.contentOffset 
    let missingSections = NSMutableIndexSet() 

    for layoutAttributes in superAttributes { 
     if (layoutAttributes.representedElementCategory == .Cell) { 
      missingSections.addIndex(layoutAttributes.indexPath.section) 
     } 

     if let representedElementKind = layoutAttributes.representedElementKind { 
      if representedElementKind == UICollectionElementKindSectionHeader { 
       missingSections.removeIndex(layoutAttributes.indexPath.section) 
      } 
     } 
    } 

    missingSections.enumerateIndexesUsingBlock { idx, stop in 
     let indexPath = NSIndexPath(forItem: 0, inSection: idx) 
     if let layoutAttributes = self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: indexPath) { 
      superAttributes.append(layoutAttributes) 
     } 
    } 

    for layoutAttributes in superAttributes { 
     if let representedElementKind = layoutAttributes.representedElementKind { 
      if representedElementKind == UICollectionElementKindSectionHeader { 
       let section = layoutAttributes.indexPath.section 
       let numberOfItemsInSection = collectionView!.numberOfItemsInSection(section) 

       let firstCellIndexPath = NSIndexPath(forItem: 0, inSection: section) 
       let lastCellIndexPath = NSIndexPath(forItem: max(0, (numberOfItemsInSection - 1)), inSection: section) 

       var firstCellAttributes:UICollectionViewLayoutAttributes 
       var lastCellAttributes:UICollectionViewLayoutAttributes 

       if (self.collectionView!.numberOfItemsInSection(section) > 0) { 
        firstCellAttributes = self.layoutAttributesForItemAtIndexPath(firstCellIndexPath)! 
        lastCellAttributes = self.layoutAttributesForItemAtIndexPath(lastCellIndexPath)! 
       } else { 
        firstCellAttributes = self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: firstCellIndexPath)! 
        lastCellAttributes = self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionFooter, atIndexPath: lastCellIndexPath)! 
       } 

       let headerHeight = CGRectGetHeight(layoutAttributes.frame) 
       var origin = layoutAttributes.frame.origin 

       origin.y = min(max(contentOffset.y, (CGRectGetMinY(firstCellAttributes.frame) - headerHeight)), (CGRectGetMaxY(lastCellAttributes.frame) - headerHeight)) 
       ; 

       layoutAttributes.zIndex = 1024; 
       layoutAttributes.frame = CGRect(origin: origin, size: layoutAttributes.frame.size) 

      } 
     } 
    } 

    return superAttributes 
} 
2

Swift 2,2 версия тестировалось на основе ответа GregP в. Код Грега бросал дополнительную ошибку при lastCellIndexPath, потому что номер раздела изначально равен нулю.Поэтому я переместил номер numberOfItemsInSection> 0.

override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 

    let superAttributes:NSMutableArray = NSMutableArray(array: super.layoutAttributesForElementsInRect(rect)!) as NSMutableArray 

    let contentOffset = collectionView!.contentOffset 
    let missingSections = NSMutableIndexSet() 

    for layoutAttributes in superAttributes { 
     if (layoutAttributes.representedElementCategory == .Cell) { 
      if let _ = layoutAttributes.indexPath { 
       missingSections.addIndex(layoutAttributes.indexPath.section) 
      } 
     } 
    } 

    for layoutAttributes in superAttributes{ 
     if let representedElementKind = layoutAttributes.representedElementKind { 
      if representedElementKind == UICollectionElementKindSectionHeader { 
       if let indexPath = layoutAttributes.indexPath { 
        missingSections.removeIndex(indexPath.section) 
       } 
      } 
     } 
    } 

    missingSections.enumerateIndexesUsingBlock { idx, stop in 
     let indexPath = NSIndexPath(forItem: 0, inSection: idx) 
     let layoutAttributes = self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: indexPath) 
     superAttributes.addObject(layoutAttributes!) 
    } 

    for la in superAttributes { 

     let layoutAttributes = la as! UICollectionViewLayoutAttributes; 

     if let representedElementKind = layoutAttributes.representedElementKind { 
      if representedElementKind == UICollectionElementKindSectionHeader { 
       let section = layoutAttributes.indexPath.section 
       let numberOfItemsInSection = collectionView!.numberOfItemsInSection(section) 

       if numberOfItemsInSection > 0{ 
        let firstCellIndexPath = NSIndexPath(forItem: 0, inSection: section) 
        let lastCellIndexPath = NSIndexPath(forItem: max(0, (numberOfItemsInSection - 1)), inSection: section) 

        var firstCellAttributes:UICollectionViewLayoutAttributes 
        var lastCellAttributes:UICollectionViewLayoutAttributes 

        firstCellAttributes = self.layoutAttributesForItemAtIndexPath(firstCellIndexPath)! 
        lastCellAttributes = self.layoutAttributesForItemAtIndexPath(lastCellIndexPath)! 

        let headerHeight = CGRectGetHeight(layoutAttributes.frame) 
        var origin = layoutAttributes.frame.origin 

        origin.y = min(max(contentOffset.y, (CGRectGetMinY(firstCellAttributes.frame) - headerHeight)), (CGRectGetMaxY(lastCellAttributes.frame) - headerHeight)); 

        layoutAttributes.zIndex = 1024; 
        layoutAttributes.frame = CGRect(origin: origin, size: layoutAttributes.frame.size) 
       } 
      } 
     } 
    } 

    return NSArray(array: superAttributes) as? [UICollectionViewLayoutAttributes] 
} 

override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool { 
    return true 
} 
37

решение Простейшего для прошивки 9 +, поскольку это не требует написания подкласса UICollectionViewFlowLayout.

В viewDidLoad из ViewController с использованием CollectionView следующий код:

let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout // casting is required because UICollectionViewLayout doesn't offer header pin. Its feature of UICollectionViewFlowLayout 
layout?.sectionHeadersPinToVisibleBounds = true 

Он намекнул @Antoine также.

+0

Этот метод работает, но мне интересно, почему секвенсор выталкивается, когда он встречает нижний колонтитул. – JayVDiyk

+0

Это потрясающе! Лучшее решение. – SeanRobinson159

+0

Определенно самое прямое и легко работающее решение. – lwdthe1

1

Swift 3 версия пытается избежать !, где это имело смысл.

class StickyHeaderCollectionViewFlowLayout: UICollectionViewFlowLayout { 

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 
     guard var superAttributes = super.layoutAttributesForElements(in: rect), let collectionView = collectionView else { 
      return super.layoutAttributesForElements(in: rect) 
     } 

     let collectionViewTopY = collectionView.contentOffset.y + collectionView.contentInset.top 
     let contentOffset = CGPoint(x: 0, y: collectionViewTopY) 
     let missingSections = NSMutableIndexSet() 

     superAttributes.forEach { layoutAttributes in 
      if layoutAttributes.representedElementCategory == .cell && layoutAttributes.representedElementKind != UICollectionElementKindSectionHeader { 
       missingSections.add(layoutAttributes.indexPath.section) 
      } 
     } 

     missingSections.enumerate(using: { idx, stop in 
      let indexPath = IndexPath(item: 0, section: idx) 
      if let layoutAttributes = self.layoutAttributesForSupplementaryView(ofKind: UICollectionElementKindSectionHeader, at: indexPath) { 
       superAttributes.append(layoutAttributes) 
      } 
     }) 

     for layoutAttributes in superAttributes { 
      if let representedElementKind = layoutAttributes.representedElementKind { 
       if representedElementKind == UICollectionElementKindSectionHeader { 
        let section = layoutAttributes.indexPath.section 
        let numberOfItemsInSection = collectionView.numberOfItems(inSection: section) 

        let firstCellIndexPath = IndexPath(item: 0, section: section) 
        let lastCellIndexPath = IndexPath(item: max(0, (numberOfItemsInSection - 1)), section: section)     

        let cellAttributes:(first: UICollectionViewLayoutAttributes, last: UICollectionViewLayoutAttributes) = { 
         if (collectionView.numberOfItems(inSection: section) > 0) { 
          return (
           self.layoutAttributesForItem(at: firstCellIndexPath)!, 
           self.layoutAttributesForItem(at: lastCellIndexPath)!) 
         } else { 
          return (
           self.layoutAttributesForSupplementaryView(ofKind: UICollectionElementKindSectionHeader, at: firstCellIndexPath)!, 
           self.layoutAttributesForSupplementaryView(ofKind: UICollectionElementKindSectionFooter, at: lastCellIndexPath)!) 
         } 
        }() 

        let headerHeight = layoutAttributes.frame.height 
        var origin = layoutAttributes.frame.origin 
        // This line makes only one header visible fixed at the top 
//     origin.y = min(contentOffset.y, cellAttributes.last.frame.maxY - headerHeight) 
        // Uncomment this line for normal behaviour: 
        origin.y = min(max(contentOffset.y, cellAttributes.first.frame.minY - headerHeight), cellAttributes.last.frame.maxY - headerHeight) 

        layoutAttributes.zIndex = 1024 
        layoutAttributes.frame = CGRect(origin: origin, size: layoutAttributes.frame.size) 
       } 
      } 
     } 

     return superAttributes 
    } 

    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool { 
     return true 
    } 
} 
Смежные вопросы