AsyncDisplayKey - stacklayout不会在屏幕上显示

时间:2017-08-01 10:37:20

标签: asyncdisplaykit

我尝试在cellnode中创建stacklayout,但屏幕上没有显示

class Node: ASCellNode {

    let blackNode = ASDisplayNode()
    let blueNode = ASDisplayNode()

    override init() {
        super.init()
        self.addSubnode(blueNode)
        self.addSubnode(blackNode)
    }

    override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
        blackNode.backgroundColor = .black
        blueNode.backgroundColor = .blue


        let contentStackSpec = ASStackLayoutSpec(direction: .horizontal,
                                                spacing: 40,
                                                justifyContent: .start,
                                                alignItems: .center,
                                                children: [blackNode, blueNode])

        contentStackSpec.style.minWidth = ASDimensionMakeWithPoints(60.0);
        contentStackSpec.style.maxHeight = ASDimensionMakeWithPoints(40.0);

        return ASRelativeLayoutSpec(horizontalPosition: .center, verticalPosition: .center, sizingOption: .init(rawValue: 0), child: contentStackSpec)
    }
}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我认为您应该使用ASInsetLayoutSpec来显示ASStackLayoutSpec而不是ASRelativeLayoutSpec

以下是使用ASInsetLayoutSpec显示ASStackLayoutSpec的给定代码的实现:

override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
        blackNode.backgroundColor = .black
        blueNode.backgroundColor = .blue


        let contentStackSpec = ASStackLayoutSpec(direction: .horizontal,
                                                spacing: 40,
                                                justifyContent: .start,
                                                alignItems: .center,
                                                children: [blackNode, blueNode])

        contentStackSpec.style.minWidth = ASDimensionMakeWithPoints(60.0);
        contentStackSpec.style.maxHeight = ASDimensionMakeWithPoints(40.0);

        let cardInsetSpec : ASInsetLayoutSpec = ASInsetLayoutSpec(insets: UIEdgeInsetsMake(5, 5, 5, 5), child: contentStackSpec)  

        return cardInsetSpec

    }