首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在AsyncDisplayKit中设置节点布局

如何在AsyncDisplayKit中设置节点布局
EN

Stack Overflow用户
提问于 2017-11-03 14:02:00
回答 1查看 886关注 0票数 0

我试图为AsyncDisplayKit中的节点设置userProfile设置的布局,但没有获得所需的结果。

  1. 我有一个userProfile图像
  2. userName标签
  3. 设置按钮
  4. 文章,评论和喜欢按钮我想要设置所有节点的水平和垂直对齐。

下面是我在layoutSpecThatFits方法中的代码

代码语言:javascript
复制
override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {

    photoNode.style.preferredSize = CGSize(width: ImageSize, height: ImageSize)
    settingButton.style.preferredSize = CGSize(width: 100, height: 50)
    label.style.preferredSize = CGSize(width: 100, height: 50)
    editbutton.style.preferredSize = CGSize(width: 120, height: 40)

    settingButton.style.alignSelf = .stretch
    let layout = ASStackLayoutSpec.horizontal()
    layout.alignItems = .center
    layout.justifyContent = .spaceAround
    layout.spacing = 10

    layout.children = [photoNode,settingButton]
   let settingButtonStackLocation = ASStackLayoutSpec(direction: .vertical,
                                        spacing: 15,
                                        justifyContent: .center,
                                        alignItems: .center,
                                        children: [layout,label,editbutton])
    settingButtonStackLocation.style.flexShrink = 1.0
    settingButtonStackLocation.style.flexGrow = 1.0





  let insets = UIEdgeInsets(top: 50, left: 30, bottom: 0, right: 30)
    let inset =  ASInsetLayoutSpec(insets: insets, child: settingButtonStackLocation)

    let horizontalButtonsStack = ASStackLayoutSpec.horizontal()
    horizontalButtonsStack.children = [postsButton,bucketButton,likesButton]
    horizontalButtonsStack.spacing = 20
    let verticalStack = ASStackLayoutSpec.vertical()
    verticalStack.children = [inset,horizontalButtonsStack]
    verticalStack.spacing = 10
    let buttonInsets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
    let buttonWithInsets = ASInsetLayoutSpec(insets: buttonInsets, child: verticalStack)

    let horizontalFollowersStack = ASStackLayoutSpec.horizontal()
    horizontalFollowersStack.children = [followersButton,followingButton]
    horizontalFollowersStack.spacing = 10
    let verticalFollowers = ASStackLayoutSpec.vertical()
    verticalFollowers.children = [buttonWithInsets,horizontalFollowersStack]
    verticalFollowers.spacing = 10
    verticalFollowers.alignItems = .center

    let followersInsets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
    let followersWithInsets = ASInsetLayoutSpec(insets: followersInsets, child: verticalFollowers)


    return followersWithInsets
 }
EN

回答 1

Stack Overflow用户

发布于 2017-12-06 12:11:24

我想我为你找到了解决办法:

代码语言:javascript
复制
    [containerViewNode setLayoutSpecBlock:^ASLayoutSpec *(__kindof ASDisplayNode *node, ASSizeRange constrainedSize) {
        avatarImage.style.preferredSize = CGSizeMake(35, 35);
        settingsIcon.style.preferredSize = CGSizeMake(15, 15);
        ASCenterLayoutSpec *avatarCenterSpec = [[ASCenterLayoutSpec alloc] initWithHorizontalPosition:ASRelativeLayoutSpecPositionCenter verticalPosition:ASRelativeLayoutSpecPositionCenter sizingOption:ASRelativeLayoutSpecSizingOptionDefault child:avatarImage];
        avatarCenterSpec.style.flexBasis = ASDimensionMakeWithPoints(100);
        ASRelativeLayoutSpec *gearSpec = [[ASRelativeLayoutSpec alloc] initWithHorizontalPosition:ASRelativeLayoutSpecPositionEnd verticalPosition:ASRelativeLayoutSpecPositionStart sizingOption:ASRelativeLayoutSpecSizingOptionDefault child:settingsIcon];
        ASOverlayLayoutSpec *overlayLayoutSpec = [ASOverlayLayoutSpec overlayLayoutSpecWithChild:avatarCenterSpec overlay:gearSpec];
        ASStackLayoutSpec *mainSpec = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:0 justifyContent:ASStackLayoutJustifyContentCenter alignItems:ASStackLayoutAlignItemsStretch children:@[overlayLayoutSpec, textNodeStub]];
        return mainSpec;
    }];

看看ASOverlayLayoutSpec是如何与ASRelativeLayoutSpec一起工作的。我在github上共享的示例项目。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47097630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档