Swiftgram/AsyncDisplayKit/Layout/ASStaticLayoutSpec.mm
ricky cancro 45a555cdb5 Static layout bug?
I noticed when creating a static layout that the sizeRange of the children was being ignored. The case was:

I had an image as a child of a static layout
The image was set to have an exact range of 90x90
When the static layout was measured, the constrained size came in as 375xInf (the width of the containing node and unbounded)
The static layout computed its final size as 375x90

According to the comments in component kit's header file the static layout  "[c]omputes a size that is the union of all childrens' frames." It appeared that we are only doing that in the unbounded direction. My fix is to do it in both directions.
2015-10-02 15:56:31 -07:00

88 lines
2.5 KiB
Plaintext

/*
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#import "ASStaticLayoutSpec.h"
#import "ASLayoutSpecUtilities.h"
#import "ASLayoutOptions.h"
#import "ASInternalHelpers.h"
#import "ASLayout.h"
#import "ASStaticLayoutable.h"
@implementation ASStaticLayoutSpec
+ (instancetype)staticLayoutSpecWithChildren:(NSArray *)children
{
return [[self alloc] initWithChildren:children];
}
- (instancetype)init
{
return [self initWithChildren:@[]];
}
- (instancetype)initWithChildren:(NSArray *)children
{
if (!(self = [super init])) {
return nil;
}
self.children = children;
return self;
}
- (ASLayout *)measureWithSizeRange:(ASSizeRange)constrainedSize
{
CGSize size = {
constrainedSize.max.width,
constrainedSize.max.height
};
NSMutableArray *sublayouts = [NSMutableArray arrayWithCapacity:self.children.count];
for (id<ASLayoutable> child in self.children) {
CGSize autoMaxSize = {
constrainedSize.max.width - child.layoutPosition.x,
constrainedSize.max.height - child.layoutPosition.y
};
ASSizeRange childConstraint = ASRelativeSizeRangeEqualToRelativeSizeRange(ASRelativeSizeRangeUnconstrained, child.sizeRange)
? ASSizeRangeMake({0, 0}, autoMaxSize)
: ASRelativeSizeRangeResolve(child.sizeRange, size);
ASLayout *sublayout = [child measureWithSizeRange:childConstraint];
sublayout.position = child.layoutPosition;
[sublayouts addObject:sublayout];
}
size.width = constrainedSize.min.width;
for (ASLayout *sublayout in sublayouts) {
size.width = MAX(size.width, sublayout.position.x + sublayout.size.width);
}
size.height = constrainedSize.min.height;
for (ASLayout *sublayout in sublayouts) {
size.height = MAX(size.height, sublayout.position.y + sublayout.size.height);
}
return [ASLayout layoutWithLayoutableObject:self
size:ASSizeRangeClamp(constrainedSize, size)
sublayouts:sublayouts];
}
- (void)setChild:(id<ASLayoutable>)child forIdentifier:(NSString *)identifier
{
ASDisplayNodeAssert(NO, @"ASStackLayoutSpec only supports setChildren");
}
- (id<ASLayoutable>)childForIdentifier:(NSString *)identifier
{
ASDisplayNodeAssert(NO, @"ASStackLayoutSpec only supports children");
return nil;
}
@end