Skip to content

Commit 3589a30

Browse files
committed
Setting a width only now works correctly.
1 parent 8b42f77 commit 3589a30

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

Sources/SwiftUIKitView/UIViewContainer.swift

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,20 @@ public struct UIViewContainer<Child: UIView>: Identifiable {
3535
private var size: CGSize {
3636
switch layout {
3737
case .fixedWidth(let width):
38-
var compressedSize = UIView.layoutFittingCompressedSize
39-
compressedSize.width = width
40-
let previewSize = view.systemLayoutSizeFitting(
41-
compressedSize,
42-
withHorizontalFittingPriority: .required,
43-
verticalFittingPriority: .fittingSizeLevel
44-
)
45-
return previewSize
38+
// Set the frame of the cell, so that the layout can be updated.
39+
var newFrame = view.frame
40+
newFrame.size = CGSize(width: width, height: UIView.layoutFittingExpandedSize.height)
41+
view.frame = newFrame
42+
43+
// Make sure the contents of the cell have the correct layout.
44+
view.setNeedsLayout()
45+
view.layoutIfNeeded()
46+
47+
// Get the size of the cell
48+
let computedSize = view.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
49+
50+
// Apple: "Only consider the height for cells, because the contentView isn't anchored correctly sometimes." We use ceil to make sure we get rounded numbers and no half pixels.
51+
return CGSize(width: width, height: ceil(computedSize.height))
4652
case .fixed(let size):
4753
return size
4854
case .intrinsic:

0 commit comments

Comments
 (0)