diff --git a/AsyncDisplayKit/Layout/ASLayoutSpec.h b/AsyncDisplayKit/Layout/ASLayoutSpec.h index 4b583cb9fa..70914c4a41 100644 --- a/AsyncDisplayKit/Layout/ASLayoutSpec.h +++ b/AsyncDisplayKit/Layout/ASLayoutSpec.h @@ -63,19 +63,31 @@ NS_ASSUME_NONNULL_BEGIN @end /** - * An ASLayoutSpec subclass that can wrap a ASLayoutElement and calculates the layout of the child. + * An ASLayoutSpec subclass that can wrap one or more ASLayoutElement and calculates the layout based on the + * sizes of the children. If multiple children are provided the size of the biggest child will be used to for + * size of this layout spec. */ @interface ASWrapperLayoutSpec : ASLayoutSpec /* - * Returns an ASWrapperLayoutSpec object with the given layoutElement as child + * Returns an ASWrapperLayoutSpec object with the given layoutElement as child. */ + (instancetype)wrapperWithLayoutElement:(id)layoutElement AS_WARN_UNUSED_RESULT; /* - * Returns an ASWrapperLayoutSpec object initialized with the given layoutElement as child + * Returns an ASWrapperLayoutSpec object with the given layoutElements as children. */ -- (instancetype)initWithLayoutElement:(id)layoutElement NS_DESIGNATED_INITIALIZER; ++ (instancetype)wrapperWithLayoutElements:(NSArray> *)layoutElements AS_WARN_UNUSED_RESULT; + +/* + * Returns an ASWrapperLayoutSpec object initialized with the given layoutElement as child. + */ +- (instancetype)initWithLayoutElement:(id)layoutElement AS_WARN_UNUSED_RESULT; + +/* + * Returns an ASWrapperLayoutSpec object initialized with the given layoutElements as children. + */ +- (instancetype)initWithLayoutElements:(NSArray> *)layoutElements AS_WARN_UNUSED_RESULT; /* * Init not available for ASWrapperLayoutSpec diff --git a/AsyncDisplayKit/Layout/ASLayoutSpec.mm b/AsyncDisplayKit/Layout/ASLayoutSpec.mm index 3aa316aab2..b645b3c53b 100644 --- a/AsyncDisplayKit/Layout/ASLayoutSpec.mm +++ b/AsyncDisplayKit/Layout/ASLayoutSpec.mm @@ -315,11 +315,36 @@ ASLayoutElementStyleForwarding return self; } ++ (instancetype)wrapperWithLayoutElements:(NSArray> *)layoutElements +{ + return [[self alloc] initWithLayoutElements:layoutElements]; +} + +- (instancetype)initWithLayoutElements:(NSArray> *)layoutElements +{ + self = [super init]; + if (self) { + self.children = layoutElements; + } + return self; +} + - (ASLayout *)calculateLayoutThatFits:(ASSizeRange)constrainedSize { - ASLayout *sublayout = [self.child layoutThatFits:constrainedSize parentSize:constrainedSize.max]; - sublayout.position = CGPointZero; - return [ASLayout layoutWithLayoutElement:self size:sublayout.size sublayouts:@[sublayout]]; + NSArray *children = self.children; + NSMutableArray *sublayouts = [NSMutableArray arrayWithCapacity:children.count]; + + CGSize size = constrainedSize.min; + for (id child in children) { + ASLayout *sublayout = [child layoutThatFits:constrainedSize parentSize:constrainedSize.max]; + + size.width = MAX(size.width, sublayout.size.width); + size.height = MAX(size.height, sublayout.size.height); + + [sublayouts addObject:sublayout]; + } + + return [ASLayout layoutWithLayoutElement:self size:size sublayouts:sublayouts]; } @end