1

到目前为止,我已经使用 AsyncDisplayKit 做了很多工作,我真的很满意。不幸的是,现在我遇到了障碍。

我无法让 ASButtonNode 具有最小高度(如果这很重要,则在 ASCellNode 中)。

class SmallButtonCellNode: ASCellNode {

  let normalSmallButton = ASButtonNode()
  let selectedSmallButton = ASButtonNode()

  init() {
    super.init()    
    self.backgroundColor = UIColor.lightGrayColor()

    // .. button title and background configuration here ..

      let buttonSizeRange =
      ASRelativeSizeRangeMake(
        ASRelativeSizeMake(
          ASRelativeDimensionMakeWithPercent(0),
          ASRelativeDimensionMakeWithPoints(35.0)
        ),
        ASRelativeSizeMake(
          ASRelativeDimensionMakeWithPercent(1),
          ASRelativeDimensionMakeWithPoints(35.0)
        )
      );

      self.normalSmallButton.preferredFrameSize = CGSize(width: 111.0, height: 35.0)
      self.normalSmallButton.flexGrow = true
      self.normalSmallButton.sizeRange = buttonSizeRange
      self.addSubnode(self.normalSmallButton)

      self.selectedSmallButton.preferredFrameSize = CGSize(width: 111.0, height: 35.0)
      self.selectedSmallButton.flexGrow = true
      self.selectedSmallButton.sizeRange = buttonSizeRange
      self.addSubnode(self.selectedSmallButton)
  }

  // MARK: - Layout

  override func layoutSpecThatFits(constrainedSize: ASSizeRange) -> ASLayoutSpec {
    let spec =
      ASStackLayoutSpec(
        direction: .Horizontal,
        spacing: 20.0,
        justifyContent: .SpaceAround,
        alignItems: .Center,
        children: [self.normalSmallButton, self.selectedSmallButton]
      )

    return
      ASInsetLayoutSpec(
        insets: UIEdgeInsets(top: 20.0, left: 20.0, bottom: 20.0, right: 20.0),
        child: spec
      )
  }
}

结果是(尽管 constrainedSize 的最大高度为 100.0):

ASButtonNodes 仅适合文本高度

ASButtonNodes 仅适合文本高度。

我试过了:

  • 删除首选帧大小(无效)
  • 从更改.Center.Stretch(按钮与单元格一样高 - 插图)
  • flexGrow = true 在各个地方

我知道我可以使用将单元格高度从 100 更改为 75 .Stretch,然后按钮将自动最终变为 35 pts 高,但这不是我想要的(因为“按钮为 35 pts 高”的布局逻辑实际上是集合视图委托的一部分..)

请帮忙 :)

4

2 回答 2

0

更新的答案:

刚去asyncdisplaykit.org看到了 ASTaticLayoutSpec 的引用。它应该可以解决您的问题。在 layoutSpecThatFits() ...

spec.sizeRange = normalSmallButton.sizeRange let staticSpec1 = ASStaticLayoutSpec(children: [spec]) return staticSpec1

……

如果您的单元格足够简单,我认为仅在 calculateSizeThatFits(constrainedSize: CGSize) 中测量您的子节点会更容易、更直接。然后在 layout() 中,计算每个子节点的 .frame。

如果您想使用布局规范,我的技巧是在 init() 中的子节点上调用 .measure()。然后在 layoutSpecsThatFit 中,您可以将插入值调整为节点的计算大小与最小大小之间的差异。祝你好运。

var diffY: CGFloat = (35.0 - selectedSmallButton.calculatedSize.height) / 2

if diffY < 0 { diffY = 0 }

return ASInsetLayoutSpec( insets: UIEdgeInsets(top: diffY, left: 20.0, bottom: diffY, right: 20.0), child: spec)

于 2016-04-15T17:09:42.840 回答
0

试试 yourStackStyle.minHeight = ASDimensionMake(yourMinHeight)

于 2017-09-19T14:48:47.027 回答