Preserve the origin of the root view.

Summary: Currently, we have an issue in YogaKit where it ignores the preexisting bounds a root view. There are multiple fixes for this, but the easiest one for developer experience is to apply the preexisting bounds of the root view to it's frame after layout calculation.

Reviewed By: emilsjolander

Differential Revision: D4537173

fbshipit-source-id: ccef8b1c1398ea299b4e5710abe597378347a48d
This commit is contained in:
Dustin Shahidehpour
2017-02-09 13:05:38 -08:00
committed by Facebook Github Bot
parent ef05404bee
commit 5037c2f365
2 changed files with 41 additions and 5 deletions

View File

@@ -222,7 +222,7 @@ YG_PROPERTY(CGFloat, aspectRatio, AspectRatio)
- (void)applyLayout
{
[self calculateLayoutWithSize:self.view.bounds.size];
YGApplyLayoutToViewHierarchy(self.view);
YGApplyLayoutToViewHierarchy(self.view, YES);
}
- (CGSize)intrinsicSize
@@ -364,7 +364,7 @@ static CGFloat YGRoundPixelValue(CGFloat value)
return round(value * scale) / scale;
}
static void YGApplyLayoutToViewHierarchy(UIView *view)
static void YGApplyLayoutToViewHierarchy(UIView *view, BOOL preserveOrigin)
{
NSCAssert([NSThread isMainThread], @"Framesetting should only be done on the main thread.");
@@ -385,10 +385,11 @@ static void YGApplyLayoutToViewHierarchy(UIView *view)
topLeft.y + YGNodeLayoutGetHeight(node),
};
const CGPoint origin = preserveOrigin ? view.frame.origin : CGPointZero;
view.frame = (CGRect) {
.origin = {
.x = YGRoundPixelValue(topLeft.x),
.y = YGRoundPixelValue(topLeft.y),
.x = YGRoundPixelValue(topLeft.x + origin.x),
.y = YGRoundPixelValue(topLeft.y + origin.y),
},
.size = {
.width = YGRoundPixelValue(bottomRight.x) - YGRoundPixelValue(topLeft.x),
@@ -398,7 +399,7 @@ static void YGApplyLayoutToViewHierarchy(UIView *view)
if (!yoga.isLeaf) {
for (NSUInteger i=0; i<view.subviews.count; i++) {
YGApplyLayoutToViewHierarchy(view.subviews[i]);
YGApplyLayoutToViewHierarchy(view.subviews[i], NO);
}
}
}

View File

@@ -618,4 +618,39 @@
XCTAssertEqual(view.yoga.borderEndWidth, 7);
}
- (void)testOriginIsPreservedOnRootOfLayout {
const CGSize containerSize = CGSizeMake(200, 50);
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(10, 10, containerSize.width, containerSize.height)];
container.yoga.isEnabled = YES;
container.yoga.flexDirection = YGFlexDirectionRow;
UIView *subview1 = [[UIView alloc] initWithFrame:CGRectZero];
subview1.yoga.isEnabled = YES;
subview1.yoga.flexGrow = 1;
[container addSubview:subview1];
UIView *subview2 = [[UIView alloc] initWithFrame:CGRectZero];
subview2.yoga.isEnabled = YES;
subview2.yoga.flexGrow = 1;
subview2.yoga.flexDirection = YGFlexDirectionColumn;
subview2.yoga.marginLeft = 10;
[container addSubview:subview2];
[container.yoga applyLayout];
XCTAssertTrue(CGRectEqualToRect(container.frame, CGRectMake(10, 10, 200, 50)));
XCTAssertTrue(CGRectEqualToRect(subview1.frame, CGRectMake(0, 0, 95, 50)));
XCTAssertTrue(CGRectEqualToRect(subview2.frame, CGRectMake(105, 0, 95, 50)));
UIView *subview3 = [[UIView alloc] initWithFrame:CGRectZero];
subview3.yoga.isEnabled = YES;
subview3.yoga.alignSelf = YGAlignFlexEnd;
subview3.yoga.height = 50;
[subview2 addSubview:subview3];
[subview2.yoga applyLayout];
XCTAssertTrue(CGRectEqualToRect(subview2.frame, CGRectMake(115, 0, 85, 50)));
XCTAssertTrue(CGRectEqualToRect(subview3.frame, CGRectMake(85,0,0,50)));
}
@end