How can I enforce a minimum Height for a ASDisplayNode?

时间:2016-04-15 15:03:15

标签: asyncdisplaykit

I've done quite a lot of work using AsyncDisplayKit until now and I'm really really happy with it. Unfortunately now I've hit a road block.

I can't get a ASButtonNode to have a minimum height (in a ASCellNode if this is important).

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
      )
  }
}

The result is (although the constrainedSize has a maximum of 100.0 in height):

The ASButtonNodes are only fitting the text height

The ASButtonNodes are only fitting the text height.

I've tried:

  • removing the preferred frame size (no effect)
  • change from .Center to .Stretch (the button is as tall as the cell - insets)
  • flexGrow = true in various places

I know that I could change the cell height from 100 to 75 using .Stretch and then the buttons will automatically end up being 35 pts tall but that's not what I want (because then the layout logic for "The button is 35 pts tall" will actually be part of the collection view delegate ..)

Please help :)

2 个答案:

答案 0 :(得分:0)

更新回答:

刚到asyncdisplaykit.org并看到了对ASStaticLayoutSpec的引用。它应该解决你的问题。在layoutSpecThatFits()...

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

...

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

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

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)

答案 1 :(得分:0)

尝试yourStackStyle.minHeight = ASDimensionMake(yourMinHeight)