AsyncDisplayKit icon indicating copy to clipboard operation
AsyncDisplayKit copied to clipboard

[Question] error regarding 'Range min height must be a real positive integer.'

Open taonos opened this issue 10 years ago • 2 comments

I've spent a lot of time on this problem but just couldn't figure it out myself. I guess I just don't understand the layout specs that well. Really appreciate if someone can help me out.

I have a table view where each cell is displayed as a card (like facebook timeline). Within that card, there are two sections, vertically, implemented as stack. The top section is another horizontal stack that displays business image on the left and some detail info on the right. The size of a cell is fixed. I'd like to have the height of the top section to be a percent of the main container. Below is my code.

    public override func layoutSpecThatFits(constrainedSize: ASSizeRange) -> ASLayoutSpec {
        let businessImageRatio = ASRatioLayoutSpec(ratio: 0.6, child: businessImageNode)

        // Business stack at the top
        let businessStack = ASStackLayoutSpec(direction: .Horizontal, spacing: 8.0, justifyContent: .Start, alignItems: .Center, children: [businessImageRatio])
        businessStack.sizeRange = ASRelativeSizeRange(
            min: ASRelativeSize(
                width: ASRelativeDimension(type: .Percent, value: 1),
                height: ASRelativeDimension(type: .Percent, value: 0.75)    // this line causes crash
            ),
            max: ASRelativeSize(
                width: ASRelativeDimension(type: .Percent, value: 1),
                height: ASRelativeDimension(type: .Percent, value: 0.75)    // this line causes crash
            )
        )
        businessStack.alignSelf = ASStackLayoutAlignSelf.Stretch
        let businessStackStatic = ASStaticLayoutSpec(children: [businessStack])

        // The outmost container
        let mainContainer = ASStackLayoutSpec(
            direction: .Vertical,
            spacing: 10.0,
            justifyContent: .Start,
            alignItems: .Start,
            children: [businessStackStatic]
        )
        mainContainer.sizeRange = ASRelativeSizeRange(
            min: ASRelativeSize(
                width: ASRelativeDimensionMakeWithPercent(1),
                height: ASRelativeDimension(type: .Points, value: constrainedSize.max.width * 0.5)
            ),
            max: ASRelativeSize(
                width: ASRelativeDimensionMakeWithPercent(1),
                height: ASRelativeDimension(type: .Points, value: constrainedSize.max.width * 0.5)
            )
        )

        return ASInsetLayoutSpec(
            insets: UIEdgeInsets(top: 5 + 4, left: 7 + 4, bottom: 5 + 4, right: 7 + 4),
            child: ASStaticLayoutSpec(children: [mainContainer])
        )
    }

    public override func layout() {
        super.layout()

        card.frame = CGRect(x: 7, y: 5, width: self.calculatedSize.width - 2 * 7, height: self.calculatedSize.height - 2 * 5)
    }

Now obviously I could just implement the height in points and manually calculate the value. Since the height is fixed, it would be an easy alternative, but I would like to understand why the above approach doesn't work.

Thank you again for pointing me in the right direction.

taonos avatar Feb 04 '16 17:02 taonos

Actually I just figured out the solution. Had to go through some issues and a lot of source code.

Since business stack is inside the main container, the following will set its height as a percentage of its parent:

businessStack.flexBasis = ASRelativeDimensionMakeWithPercent(0.75)

There's no need to wrap the business stack in ASStaticLayoutSpec. Cheers!

taonos avatar Feb 04 '16 21:02 taonos

@Hxucaa thanks a lot for sharing your solution! I can understand how difficult it is to figure out this stuff without any documentation. The great news is that within a few weeks, the Ray Wenderlich website will publish a highly detailed three part series on AsyncDisplayKit 2.0, including a dedicated tutorial for ASLayoutSpec!

I will make sure that the example included something similar to what you encountered here, so that users are introduced to the powerful yet difficult to discover .flexBasis property.

Please feel free to ask any further questions or report other issues with the framework! Email me any time at [email protected], too.

appleguy avatar Feb 07 '16 06:02 appleguy