ios 纹理/异步显示工具包ASDisplayNode首选大小的宽度不起作用

eyh26e7m  于 2023-01-18  发布在  iOS
关注(0)|答案(1)|浏览(128)

我对Texture/AsyncDisplayKit非常陌生。我能够用这个简单的代码重现这个问题。
我基本上想在ASCellNode的右下角显示一个10 x10的正方形。我是通过一个ASDisplayNode来实现的,它被设置为ASCornerLayoutSpec的角。我已经将ASDisplayNodestyle.preferredSize设置为CGSize(width: 10, height: 10)
由于某种原因,宽度不起作用,显示为屏幕宽度的50%:

代码:

import UIKit
import AsyncDisplayKit

class ViewController: ASDKViewController<ASDisplayNode>, ASTableDataSource {

    var tableNode = ASTableNode()

    override init() {
        super.init(node: tableNode)
        
        node.automaticallyRelayoutOnSafeAreaChanges = true
        node.automaticallyManagesSubnodes = true
        tableNode.dataSource = self
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func tableNode(_ tableNode: ASTableNode, numberOfRowsInSection section: Int) -> Int {
        return 100
    }
    
    func tableNode(_ tableNode: ASTableNode, nodeBlockForRowAt indexPath: IndexPath) -> ASCellNodeBlock {
        let cellNodeBlock = { () -> ASCellNode in
            let cellNode = CellNode()
            return cellNode
        }
        
        return cellNodeBlock
    }
    
}

class CellNode: ASCellNode {
    fileprivate let titleNode = ASTextNode()
    fileprivate let savedIconNode = ASDisplayNode()
    
    
    override init() {
        super.init()
        self.automaticallyManagesSubnodes = true
        titleNode.attributedText = NSAttributedString(string: "This is my title!", attributes: [.font : UIFont.systemFont(ofSize: 20),.foregroundColor:UIColor.white])
        
        savedIconNode.isLayerBacked = true
        savedIconNode.backgroundColor = .green
        savedIconNode.style.preferredSize = CGSize(width: 10, height: 10)
    }
    
    override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
        let paddingToUse = 15.0;
        let contentView = ASInsetLayoutSpec(insets: UIEdgeInsets(top: paddingToUse, left: paddingToUse, bottom: paddingToUse, right: paddingToUse), child: ASStackLayoutSpec(direction: .horizontal, spacing: 5, justifyContent: .spaceBetween, alignItems: .notSet, children: [titleNode]))
        
        let contentViewAndSaveTriangle = ASCornerLayoutSpec(child: contentView, corner: savedIconNode, location: .bottomRight)
        
        return contentViewAndSaveTriangle
    }
}
jdgnovmf

jdgnovmf1#

您可以通过在ASCornerLayoutSpec构造函数中为角节点提供所需的宽度和高度来修复此问题,如下所示:

let contentViewAndSaveTriangle = ASCornerLayoutSpec(child: contentView, corner: savedIconNode, location: .bottomRight, cornerSize: CGSize(width: 10, height: 10))

ASCornerLayoutSpec不考虑savedIconNodepreferredSize,相反,角节点的大小由子节点的大小决定。

相关问题