Texture icon indicating copy to clipboard operation
Texture copied to clipboard

[ASCollectionNode] Section header/footer size calculation and contentInset.

Open heikkihautala opened this issue 6 years ago • 1 comments

Hi!

I'm having a problem with ASCollectionNode's contentInset. After hours of debugging, to me It looks like the horizontal content insets are ignored when calculating header/footer sizes. I have attached a screenshot, a sample project and a code snippet.

This bug is reproducing using Texture 2.6, 2.7 and on master branch (commit: 2d43f468d0cfbd185181c6b2512cd93936fadca1).

Sceenshot

screenshot_iphone8

Sample project

ContentInset.zip

Relevant code

import UIKit
import AsyncDisplayKit

class ViewController: ASViewController<ASDisplayNode> {

    private let layout = UICollectionViewFlowLayout()
    private let collection: ASCollectionNode

    init() {
        collection = ASCollectionNode(collectionViewLayout: layout)
        collection.registerSupplementaryNode(ofKind: UICollectionView.elementKindSectionHeader)
        super.init(node: collection)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        edgesForExtendedLayout = []
        layout.minimumLineSpacing = 20
        layout.minimumInteritemSpacing = 20
        layout.scrollDirection = .vertical
        collection.contentInset = UIEdgeInsets(top: 0, left: 57, bottom: 0, right: 57)
        collection.delegate = self
        collection.dataSource = self

    }
}

class HeaderNode: ASCellNode {

    private let titleNode = ASTextNode()

    init(title: String, fontSize: CGFloat) {
        super.init()
        let attributedText = NSAttributedString(string: title, attributes: [
            .font: UIFont.systemFont(ofSize: fontSize)
            ])
        titleNode.attributedText = attributedText
        addSubnode(titleNode)
    }

    override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
        return ASStackLayoutSpec(direction: .vertical, spacing: 0, justifyContent: .center, alignItems: .center, children: [titleNode])
    }
}

extension ViewController: ASCollectionDataSource {
    func numberOfSections(in collectionNode: ASCollectionNode) -> Int {
        return 40
    }

    func collectionNode(_ collectionNode: ASCollectionNode, numberOfItemsInSection section: Int) -> Int {
        return 2
    }

    func collectionNode(_ collectionNode: ASCollectionNode, nodeForItemAt indexPath: IndexPath) -> ASCellNode {
        let cell = ASCellNode()
        cell.style.width = ASDimension(unit: .points, value: 140)
        cell.style.height = ASDimension(unit: .points, value: 140)
        cell.backgroundColor = .red
        return cell
    }

    func collectionNode(_ collectionNode: ASCollectionNode,
                        nodeForSupplementaryElementOfKind kind: String,
                        at indexPath: IndexPath) -> ASCellNode {
        if kind == UICollectionView.elementKindSectionHeader {
            return HeaderNode(title: "Header text", fontSize: CGFloat(20 + indexPath.section * 5))
        } else {
            let emptyNode: ASCellNode = ASCellNode()
            emptyNode.style.minSize = CGSize(width: 0.01, height: 0.01)
            return emptyNode
        }
    }
}

extension ViewController: ASCollectionDelegateFlowLayout {

    func collectionNode(_ collectionNode: ASCollectionNode, sizeRangeForHeaderInSection section: Int) -> ASSizeRange {
        return ASSizeRangeUnconstrained
    }
}

heikkihautala avatar Jan 14 '19 05:01 heikkihautala

+1

AugustRush avatar Sep 01 '23 09:09 AugustRush