如何在AsyncDisplayKit中布局ASDisplayNode的父级?

时间:2017-05-28 14:19:57

标签: ios objective-c asyncdisplaykit

我想在这张照片上做: https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

我做templateDisplayNode:

@interface TemplateDisplayNode : ASDisplayNode

    @property (nonatomic, strong) ASDisplayNode *headerNode;
    @property (nonatomic, strong) ASDisplayNode *ContentNode;
    - (void)addContentNode:(ASDisplayNode *)contentNode;

    @end

    @implement TemplateDisplayNode

    - (instancetype)init
    {
        self = [super init];
        if (self) {
            [self createSubviews];
        }
        return self;
    }

    - (void)createSubviews {
        [self createHeaderNode];
        [self createContentNode];
    }

    - (void)addContentNode:(ASDisplayNode *)contentNode {
        [self.contentNode addSubnode:contentNode];
    }

    - (void)createHeaderNode {
        self.headerNode = [[ASDisplayNode alloc] init];
        self.headerNode.backgroundColor = [UIColor blueColor];
        [self addSubnode:self.headerNode];
    }

    - (void)createContentNode {
        self.contentNode = [[ASDisplayNode alloc] init];
        self.contentNode.style.flexGrow = YES;
        [self addSubnode:self.contentNode];
    }

    - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize {

        CGFloat mainScreenWidth = [UIScreen mainScreen].bounds.size.width;

        NSMutableArray *mainStackContent = [[NSMutableArray alloc] init];

        if (self.headerNode) { 

            self.headerNode.style.preferredSize = CGSizeMake(mainScreenWidth, 48);
            [mainStackContent addObject:self.headerNode];
        }
        if (self.contentNode) {
            [mainStackContent addObject:self.contentNode];
        }
        ASStackLayoutSpec *contentSpec =
        [ASStackLayoutSpec
         stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical
         spacing:0
         justifyContent:ASStackLayoutJustifyContentStart
         alignItems:ASStackLayoutAlignItemsStretch
         children:mainStackContent];

        return contentSpec;
    }

    @end

并创建TemplateDisplayNode类的页面:

@interface Page1Node : TemplateDisplayNode

@end

@implement Page1Node

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize {
    [super layoutSpecThatFits:constrainedSize];

}

@end

并在ASViewController中使用它:

@interface MyViewController : ASViewController

@property (nonatomic, strong) MyCustomASViewControllerWithTableNode *myTableNodeViewController;
@property (nonatomic, strong) Page1Node *page1node;

@end

@implement MyViewController

- (instancetype)init {
    self.page1node = [[Page1Node alloc] init];
    self = [super initWithNode:self.page1node];

    if (self) {
        [self createMyTableNodeViewController];
    }

    return self;
}

- (void)createMyTableNodeViewController {
    self.myTableNodeViewController = [[MyCustomASViewControllerWithTableNode alloc] init];
    [self.page1node addContentNode:self.myTableNodeViewController.node]; //add TableNode like content on page1node
    [self addChildViewController:self.myTableNodeViewController];
    [self.myTableNodeViewController didMoveToParentViewController:self];
}

@end

MyCustomASViewControllerWithTableNode:

@interface MyCustomASViewControllerWithTableNode : ASViewController
@property (nonatomic, strong) ASTableNode *tableNode;
@end
...
- (instancetype)init {
    _tableNode = [[ASTableNode alloc] initWithStyle:UITableViewStylePlain];
    self = [super initWithNode:_tableNode];

    if (self) {
        _tableNode.dataSource = self;
        _tableNode.delegate = self;
    }

    return self;
}

当我运行它时,我在内容字段中看到一个表,但是我无法滚动表,因为Size frame ContentNode = {0; 0}; 如何正确设置布局以设置大小?如果我做同样的事情,但没有使用TemplateDisplayNode,那么一切正常。我想使用TemplateDisplayNode删除类似页面的大量重复代码。 建议不要使用AsyncDisplayKit文档:

[super layoutSpecThatFits:constrainedSize];

如果我删除此行,那么我不会进入TemplateDisplayNode中的layoutSpecThatFits。 建议怎么样?可以有什么解决方案?

0 个答案:

没有答案