Fix layout subview with wrap size getting zero height/width in parent layout #58
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When a layout view (e.g.
TGLinearLayout) is used as a subview of another layout with wrap width or height (tg_width.equal(.wrap)/tg_height.equal(.wrap)), the sublayout gets a frame of(0, 0, width, 0)or(0, 0, 0, height)instead of its content size.Cause:
tgCalcSizeFromSizeWrapSubviewinTGBaseLayout.swiftonly handles non-layout views. For subviews that areTGBaseLayoutwith wrap size, it never callstg_sizeThatFitsto compute their size, sosbvtgFramestays at the initial(0,0,0,0). The parent then uses this zero size when assigning the sublayout’s frame.Solution
In
tgCalcSizeFromSizeWrapSubview, before the existing non-layout branch:TGBaseLayoutand has wrap width or height (sbvsc.isSomeSizeWrap):tg_sizeThatFits(sbvtgFrame.frame.size)to get the sublayout’s size.sbvtgFrame.widthand/orsbvtgFrame.heightwith the returned size when the corresponding dimension is wrap.This way the parent layout gets the correct size for the layout subview and no longer assigns zero height or width.
Code change
File:
TangramKit/TGBaseLayout.swiftFunction:
tgCalcSizeFromSizeWrapSubview(_:sbvsc:sbvtgFrame:)Add the following block right after the
tg_visibility == .goneearly return and before the existing “只有非布局视图才这样处理” block:How to verify
TGLinearLayout, add a childTGLinearLayout(.vert)withtg_size(width:.fill, height: .wrap)and add several subviews with fixed heights to the child.0.Checklist
tgCalcSizeFromSizeWrapSubview; no API changes.tg_sizeThatFits.