CKComponent() Category Reference

Conforms to CKTreeNodeComponentProtocol
Declared in CKComponentInternal.h
CKComponentSubclass.h

Overview

A constant that indicates that the parent’s size is not yet determined in either dimension.

Other Methods

– mountInContext:layout:supercomponent:

Mounts the component in the given context: - Stores references to the supercomponent and superview for -nextResponder and -viewConfiguration. - Creates or updates a controller for this component, if one should exist. - If this component has a view, creates or recycles a view by fetching one from the given MountContext, and: - Unmounts the view’s previous component (if any). - Applies attributes to the view. - Stores a reference to the view in _mountedView (for -viewContext, transient view state, and -unmount). - Stores a reference back to this component using CKSetMountedComponentForView. (This sets up a retain cycle which must be torn down in -unmount.)

- (CK : : Component : : MountResult)mountInContext:(const CK : : Component : : MountContext &)context layout:(const CKLayout &)layout supercomponent:(CKComponent *)supercomponent

Parameters

context

The component’s content should be positioned within the given view at the given position.

layout

The layout that is being mounted

supercomponent

This component’s parent component

Return Value

An updated mount context. In most cases, this is just be the passed-in context. If a view was created, this is used to specify that subcomponents should be mounted inside the view.

Discussion

Override this if your component wants to perform a custom mounting action, but this should be very rare!

Declared In

CKComponentInternal.h

– accessibilityChildren

This method can be used to override what accessible elements are provided by the component. Very similar to UIKit accessibilityElements.

- (NSArray<NSObject*> *)accessibilityChildren

Discussion

Override this if your component wants needs to return a custom set of accessible children, but this should be very rare!

Declared In

CKComponentInternal.h

– setViewConfiguration:

For internal use only; don’t use this directly.

- (void)setViewConfiguration:(const CKComponentViewConfiguration &)viewConfiguration

Declared In

CKComponentInternal.h

– viewConfiguration

A CKComponentViewConfiguration specifies the class of a view and the attributes that should be applied to it.

- (const CKComponentViewConfiguration &)viewConfiguration

Declared In

CKComponentInternal.h

  metadata

Free form metadata associated with the component

@property (nonatomic, readonly) NSDictionary<NSString*id> *metadata

Declared In

CKComponentInternal.h

  rootComponentMountedView

Used to get the root component in the responder chain; don’t touch this.

@property (nonatomic, weak) UIView *rootComponentMountedView

Declared In

CKComponentInternal.h

  size

The size that was passed into the component; don’t touch this.

@property (nonatomic, assign, readonly) CKComponentSize size

Declared In

CKComponentInternal.h

  scopeEnumeratorProvider

Used to get the scope root enumerator; during component creation only

@property (nonatomic, strong, readonly) id<CKComponentScopeEnumeratorProvider> scopeEnumeratorProvider

Declared In

CKComponentInternal.h

  backtraceStackDescription

For internal debug use only; don’t touch this.

@property (nonatomic, copy, readonly) NSString *backtraceStackDescription

Declared In

CKComponentInternal.h

  hasAnimations

For internal use; don’t touch this.

@property (nonatomic, assign, readonly) BOOL hasAnimations

Declared In

CKComponentInternal.h

  hasBoundsAnimations

For internal use; don’t touch this.

@property (nonatomic, assign, readonly) BOOL hasBoundsAnimations

Declared In

CKComponentInternal.h

  hasInitialMountAnimations

For internal use; don’t touch this.

@property (nonatomic, assign, readonly) BOOL hasInitialMountAnimations

Declared In

CKComponentInternal.h

  hasFinalUnmountAnimations

For internal use; don’t touch this.

@property (nonatomic, assign, readonly) BOOL hasFinalUnmountAnimations

Declared In

CKComponentInternal.h

  controllerOverridesDidPrepareLayout

For internal use; don’t touch this.

@property (nonatomic, assign, readonly) BOOL controllerOverridesDidPrepareLayout

Declared In

CKComponentInternal.h

Other Methods

– layoutThatFits:parentSize:

Call this on children components to compute their layouts within your implementation of -computeLayoutThatFits:.

- (CKLayout)layoutThatFits:(CKSizeRange)constrainedSize parentSize:(CGSize)parentSize

Parameters

constrainedSize

Specifies a minimum and maximum size. The receiver must choose a size that is in this range.

parentSize

The parent component’s size. If the parent component does not have a final size in a given dimension, then it should be passed as kCKComponentParentDimensionUndefined (for example, if the parent’s width depends on the child’s size).

Return Value

A struct defining the layout of the receiver and its children.

Discussion

Warning: You may not override this method. Override -computeLayoutThatFits: instead.

Warning: In almost all cases, prefer the use of CKComputeComponentLayout in CKLayout

Declared In

CKComponentSubclass.h

– computeLayoutThatFits:

Override this method to compute your component’s layout.

- (CKLayout)computeLayoutThatFits:(CKSizeRange)constrainedSize

Parameters

constrainedSize

A min and max size. This is computed as described in the description. The CKLayout you return MUST have a size between these two sizes. This is enforced by assertion.

Discussion

Why do you need to override -computeLayoutThatFits: instead of -layoutThatFits:parentSize:? The base implementation of -layoutThatFits:parentSize: does the following for you: 1. First, it uses the parentSize parameter to resolve the component’s size (the one passed into -initWithView:size:). 2. Then, it intersects the resolved size with the constrainedSize parameter. If the two don’t intersect, constrainedSize wins. This allows a component to always override its children’s sizes when computing its layout. (The analogy for UIView: you might return a certain size from -sizeThatFits:, but a parent view can always override that size and set your frame to any size.)

Declared In

CKComponentSubclass.h

– computeLayoutThatFits:restrictedToSize:relativeToParentSize:

CKComponent’s implementation of -layoutThatFits:parentSize: calls this method to resolve the component’s size against parentSize, intersect it with constrainedSize, and call -computeLayoutThatFits: with the result.

- (CKLayout)computeLayoutThatFits:(CKSizeRange)constrainedSize restrictedToSize:(const CKComponentSize &)size relativeToParentSize:(CGSize)parentSize

Discussion

In certain advanced cases, you may want to customize this logic. Overriding this method allows you to receive all three parameters and do the computation yourself.

Warning: Overriding this method should be done VERY rarely.

Declared In

CKComponentSubclass.h

– state

Returns the component’s state if available. Can be called only after the component’s creation is done.

- (id _Nullable)state

Declared In

CKComponentSubclass.h

– updateState:mode:

Enqueue a change to the state.

- (void)updateState:(id _Nullable ( ^ ) ( id _Nullable currentState ))updateBlock mode:(CKUpdateMode)mode

Parameters

updateBlock

A block that takes the current state as a parameter and returns an instance of the new state.

mode

The update mode used to apply the state update. @@see CKUpdateMode

Discussion

The state must be immutable since components themselves are. A possible use might be:

[self updateState:^MyState (MyState currentState) { MyMutableState *nextState = [currentState mutableCopy]; [nextState setFoo:[nextState bar] * 2]; return [nextState copy]; // immutable! :D }];

Declared In

CKComponentSubclass.h

– targetForAction:withSender:

Allows an action to be forwarded to another target. By default, returns the receiver if it implements action, and proceeds up the responder chain otherwise.

- (id _Nullable)targetForAction:(SEL _Nullable)action withSender:(id _Nullable)sender

Declared In

CKComponentSubclass.h

– canPerformAction:withSender:

When an action is triggered, a component may use this method to either capture or ignore the given action. The default implementation simply uses respondsToSelector: to determine if the component can perform the given action.

- (BOOL)canPerformAction:(SEL _Nullable)action withSender:(id _Nullable)sender

Discussion

In practice, this is useful only for integrations with UIMenuController whose API walks the UIResponder chain to determine which menu items to display. You should not override this method for standard component actions.

Declared In

CKComponentSubclass.h

– animationsOnInitialMount

Override to return a list of animations that will be applied to the component when it is first mounted.

- (std : : vector<CKComponentAnimation>)animationsOnInitialMount

Discussion

Warning: If you override this method, your component MUST declare a scope (see CKComponentScope). This is used to identify equivalent components between trees.

Declared In

CKComponentSubclass.h

– animationsFromPreviousComponent:

Override to return a list of animations from the previous version of the same component.

- (std : : vector<CKComponentAnimation>)animationsFromPreviousComponent:(CKComponent *)previousComponent

Discussion

Warning: If you override this method, your component MUST declare a scope (see CKComponentScope). This is used to identify equivalent components between trees.

Declared In

CKComponentSubclass.h

– boundsAnimationFromPreviousComponent:

Override to return how the change to the bounds of the root component should be animated when updating the hierarchy.

- (CKComponentBoundsAnimation)boundsAnimationFromPreviousComponent:(CKComponent *)previousComponent

Discussion

Warning: If you override this method, your component MUST declare a scope (see CKComponentScope). This is used to identify equivalent components between trees.

Declared In

CKComponentSubclass.h

– animationsOnFinalUnmount

Experimental API, do not use

- (std : : vector<CKComponentFinalUnmountAnimation>)animationsOnFinalUnmount

Discussion

Override to return the list of component / animation pairs that will run when this component is no longer in the mounted hierarchy. You can return multiple animations for this component or specify animations for any child components.

Warning: If you override this method, your component MUST declare a scope (see CKComponentScope). This is used to identify equivalent components between trees.

Declared In

CKComponentSubclass.h

  viewForAnimation

Attempts to return a view suitable for rendering an animation.

@property (nonatomic, strong, readonly, nullable) UIView *viewForAnimation

Discussion

Since a component may or may not be backed by a view nil may be returned. Composite components may, given the fact they are composed of other components, return the animatable view of its descendant. As a rule of thumb:

  1. CKComponent subclasses backed by a view will return the backing view
  2. CKComponent subclasses not backed by a view will return nil
  3. CKCompositeComponent subclasses backed by a view will return the backing view
  4. CKCompositeComponent subclasses not backed by a view will return the animatable view of its descendant

This method may be overridden in rare situations where a more suitable view should be used for rendering animations.

Declared In

CKComponentSubclass.h

– controller

Returns the component’s controller, if any.

- (CKComponentController *_Nullable)controller

Declared In

CKComponentSubclass.h