Problem with ASCollectionNode size inside ASDisplayNode
I have a problem with ASCollectionNode size when I use it inside ASDisplayNode. In current case ASCN it description is:
<ASCollectionView: 0x7fe6e5859e00; baseClass = UICollectionView; frame = (187.5 326.5; 0 0); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x600002647ba0>; layer = <EtagiClient.TestCC-Layer: 0x600002647600; node = <EtagiClient.TestCC: 0x7fe6e555ac50>>; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}> collection view layout: <UICollectionViewFlowLayout: 0x7fe6e5559a10>
But, if I use TestCC as a root node of TestVC I don't have this problem, size calculated and applying.
Can you help me, please?
Source test code:
import AsyncDisplayKit
class TestVC: ASViewController<ASDisplayNode> {
init() {
super.init(node: TestDN())
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class TestDN: ASDisplayNode {
var collectionNode: TestCC?
var textNode: ASTextNode?
override func didLoad() {
automaticallyManagesSubnodes = true
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.minimumLineSpacing = 0.0
layout.minimumInteritemSpacing = 0.0
layout.sectionInset = .zero
layout.headerReferenceSize = .zero
layout.footerReferenceSize = .zero
collectionNode = TestCC(collectionViewLayout: layout)
collectionNode!.backgroundColor = UIColor.blue
textNode = ASTextNode()
textNode!.attributedText = NSAttributedString(string: "Test string")
}
override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
return ASStackLayoutSpec(direction: .vertical, spacing: 0.0, justifyContent: .center, alignItems: .center, children: [collectionNode!, textNode!])
}
}
class TestCC: ASCollectionNode {
public override init(collectionViewLayout: UICollectionViewLayout) {
super.init(frame: CGRect.zero, collectionViewLayout: collectionViewLayout, layoutFacilitator: nil)
dataSource = self
delegate = self
view.isPagingEnabled = true
backgroundColor = UIColor.white
contentMode = .scaleToFill
view.bounces = false
}
}
class TestCellNode: ASCellNode {
let imageNode = ASNetworkImageNode()
let text1 = ASTextNode()
let text2 = ASTextNode()
required init(url: String) {
super.init()
imageNode.defaultImage = #imageLiteral(resourceName: "noImage")
imageNode.url = URL(string: url)
text1.attributedText = NSAttributedString(string: "TEXT 1")
text2.attributedText = NSAttributedString(string: "TEXT 2")
addSubnode(imageNode)
addSubnode(text1)
addSubnode(text2)
}
override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
let ratioValue: CGFloat = 3.0/4.0
let imageRatioSpec = ASRatioLayoutSpec(ratio: ratioValue, child: self.imageNode)
return ASStackLayoutSpec(direction: .vertical,
spacing: 0.0,
justifyContent: .center,
alignItems: .center,
children: [imageRatioSpec, text1, text2])
}
}
extension TestCC: ASCollectionDataSource {
func numberOfSections(in collectionNode: ASCollectionNode) -> Int {
return 1
}
func collectionNode(_ collectionNode: ASCollectionNode, nodeForItemAt indexPath: IndexPath) -> ASCellNode {
return TestCellNode(url: "")
}
func collectionNode(_ collectionNode: ASCollectionNode, numberOfItemsInSection section: Int) -> Int {
return 5
}
}
extension TestCC: ASCollectionDelegate {
}
I use Texture (2.7)
@guzhavin Hi! Did you find a solve of your issue?
@guzhavin Hi! Did you find a solve of your issue?
We dropped it. Now use Autolayout. Sorry)
@guzhavin @atkalikov
The ASCollectionNode has no intrinsic content size when it is created, so you have to set the size of it.
You can use the style.preferredSize :
collectionNode.style.preferredSize = CGSize(width: 205, height: 115)
or user ASRatioLayoutSpec.
When you use ASCollectionNode as root node of ViewController, it will set the size of ASCollectionNode.
@dirtmelon But what should I do if collection's height (or width) is dynamic?
@atkalikov
Update the preferredSize of ASCollectionNode and call setNeedsLayout.