ios - 如何在 AsyncDisplayKit 中设置节点布局

标签 ios swift asyncdisplaykit

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

  1. 我有一个用户个人资料图片
  2. 用户名标签
  3. 设置按钮
  4. 帖子、评论和点赞按钮 我想将所有节点设置为水平和垂直对齐

这是我在 layoutSpecThatFits 方法中的代码

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
 }

最佳答案

我想我找到了适合你的解决方案:

    [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 上分享的示例项目.

关于ios - 如何在 AsyncDisplayKit 中设置节点布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47097630/

相关文章:

ios - 初始化后如何更改按钮的位置?

ios - 如何隐藏按钮 5 秒

ios - 下载启用 Fairplay 的视频时获取服务器播放上下文时出错

ios - UIView 即使在开始从 superview 中移除后也会重新出现

ios - iPhone 模拟器 10.0 - 语音识别不可用

swift - 如何从 Swift 访问 Bluemix Conversation 变量

ios - UIPageViewController 的第一页显示黑屏且未加载内容

ios - GPUImageMovie 在应用滤镜时暂停

ios - 在子节点的显示隐藏上快速重新调整/调整布局(ASDisplayNode)

ios - 在水平 ASStackLayout 中垂直居中节点