iOS - Липкие заголовки со вставкой контента - Вид заголовка не прокручивается как ячейка - PullRequest
0 голосов
/ 03 марта 2019

Мы пытались навести UICollectionView как список по карте.Это не должно быть перепутано с нижним листом, который привязывается к точке (как низкий, средний и высокий).Для свойства flowlayout коллекции collectionview включена sectionHeadersPinToVisibleBounds.Я приложил образец проекта для вашей справки.Есть ли способ, которым представление заголовка может перемещаться в верхнюю часть представления коллекции при прокрутке пользователя?

Здесь - это пример проекта

Существенные изменения, необходимые для меня, чтобы войтиэто состояние:

let layout = UICollectionViewFlowLayout()
layout.sectionHeadersPinToVisibleBounds = true
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0

let collectionView = UICollectionView(frame: .zero,
                                              collectionViewLayout: layout)


override func viewWillLayoutSubviews() {
  super.viewWillLayoutSubviews()

  collectionView.contentInset = UIEdgeInsets(top: drawerHeight, left: 0, bottom: 0, right: 0)
}

Вот скриншот того, что вы увидите: enter image description here

Представление красного цвета - это закрепленный заголовок.Нужен ли пользовательский макет для обновления его позиции при прокрутке пользователя?

1 Ответ

0 голосов
/ 03 марта 2019

Я написал собственный StickyHeaderLayout, вдохновленный этим постом .Вот исправление моей ошибки:

class StickyHeaderLayout: UICollectionViewFlowLayout {

    override init() {
        super.init()
        configureLayout()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        configureLayout()
    }

    private func configureLayout() {
        self.sectionFootersPinToVisibleBounds = true
        self.sectionHeadersPinToVisibleBounds = true
        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
    }

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

        for attribute in attributes {
            adjustAttributesIfNeeded(attribute)
        }
        return attributes
    }

    override func layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        guard let attributes = super.layoutAttributesForSupplementaryView(ofKind: elementKind, at: indexPath) else { return nil }
        adjustAttributesIfNeeded(attributes)
        return attributes
    }

    func adjustAttributesIfNeeded(_ attributes: UICollectionViewLayoutAttributes) {
        switch attributes.representedElementKind {
        case UICollectionView.elementKindSectionHeader?:
            adjustHeaderAttributesIfNeeded(attributes)
        default:
            break
        }
    }

    private func adjustHeaderAttributesIfNeeded(_ attributes: UICollectionViewLayoutAttributes) {
        guard let collectionView = collectionView else { return }
        guard attributes.indexPath.section == 0 else { return }

        if collectionView.contentOffset.y <= 0 {
            attributes.frame.origin.y = 0
        } else {
            attributes.frame.origin.y = collectionView.contentOffset.y
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...