Avoid using UIView::frame in favor of bounds/center #691

Closed
lelandrichardson wants to merge 1 commits from lmr--avoid-using-uiview-frame into main

View File

@@ -451,15 +451,26 @@ static void YGApplyLayoutToViewHierarchy(UIView *view, BOOL preserveOrigin)
topLeft.y + YGNodeLayoutGetHeight(node),
};
const CGPoint origin = preserveOrigin ? view.frame.origin : CGPointZero;
view.frame = (CGRect) {
.origin = {
.x = YGRoundPixelValue(topLeft.x + origin.x),
.y = YGRoundPixelValue(topLeft.y + origin.y),
},
const CGPoint origin = preserveOrigin ? (CGPoint){
.x = view.center.x - (view.bounds.size.width / 2),
.y = view.center.y - (view.bounds.size.height / 2),
} : CGPointZero;
CGFloat x = topLeft.x + origin.x;
CGFloat y = topLeft.y + origin.y;
CGFloat width = bottomRight.x - topLeft.x;
CGFloat height = bottomRight.y - topLeft.y;
view.center = (CGPoint) {
.x = YGRoundPixelValue(x + (width / 2)),
.y = YGRoundPixelValue(y + (height / 2)),
};
view.bounds = (CGRect) {
.origin = CGPointZero,
lucdion commented 2018-01-20 11:23:24 -08:00 (Migrated from github.com)
Review

Hi @lelandrichardson, to handle correctly the view's layer.anchorPoint and to keep the view's bounds.origin, you should update your code to something like that. This thing is little tricky. UIScrollView's bounds.origin is not .zero when a contentOffset is set.

// NOTE: The center is offset by the layer.anchorPoint, so we have to take it into account.
    view.center = (CGPoint) {
        .x = YGRoundPixelValue(x + (width * view.layer.anchorPoint.x)),
        .y = YGRoundPixelValue(y + (height * view.layer.anchorPoint.y)),
    };

    // NOTE: We must set only the bounds' size and keep the origin.
    view.bounds = (CGRect) {
        .origin = view.bounds.origin,
        .size = {
            .width = YGRoundPixelValue(width),
            .height = YGRoundPixelValue(height),
        },
    };
Hi @lelandrichardson, to handle correctly the view's layer.anchorPoint and to keep the view's bounds.origin, you should update your code to something like that. This thing is little tricky. UIScrollView's bounds.origin is not .zero when a contentOffset is set. ``` // NOTE: The center is offset by the layer.anchorPoint, so we have to take it into account. view.center = (CGPoint) { .x = YGRoundPixelValue(x + (width * view.layer.anchorPoint.x)), .y = YGRoundPixelValue(y + (height * view.layer.anchorPoint.y)), }; // NOTE: We must set only the bounds' size and keep the origin. view.bounds = (CGRect) { .origin = view.bounds.origin, .size = { .width = YGRoundPixelValue(width), .height = YGRoundPixelValue(height), }, }; ```
.size = {
.width = YGRoundPixelValue(bottomRight.x) - YGRoundPixelValue(topLeft.x),
.height = YGRoundPixelValue(bottomRight.y) - YGRoundPixelValue(topLeft.y),
.width = YGRoundPixelValue(width),
.height = YGRoundPixelValue(height),
},
};