Search code examples
iosswiftasyncdisplaykit

How do I change the position of the button after initialization?


I have ASViewController<ASDisplayNode>. In init() I set into display node new node with ASAbsoluteLayoutSpec which contains two button. How I can change button position during ASViewController lifecycle?


Solution

  • Of cause, you can. Just use the variable flag or switch statement in layoutThatFits: method for change logic behavior, and after changing a flag state call just self.setNeedsLayout or transitionLayoutWithAnimation:shouldMeasureAsync:measurementCompletion: for animation, and this recommended method for change layouts. For more info read the docs Example:

    override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
    let fieldNode: FieldNode
    
      if self.fieldState == .signupNodeName { // flag dependens state
          fieldNode = self.nameField
      } else {
          fieldNode = self.ageField
      }
    
      let stack = ASStackLayoutSpec()
      stack.children = [fieldNode, buttonNode]
    
      let insets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
      return ASInsetLayoutSpec(insets: insets, child: stack)
    }
    

    for change layouts use transitionLayoutWithAnimation:shouldMeasureAsync:measurementCompletion: method call. Don't forget change a state before of this call.