Skip to content

Commit

Permalink
Setting a width only now works correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
AvdLee committed Jan 6, 2021
1 parent 8b42f77 commit 3589a30
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions Sources/SwiftUIKitView/UIViewContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,20 @@ public struct UIViewContainer<Child: UIView>: Identifiable {
private var size: CGSize {
switch layout {
case .fixedWidth(let width):
var compressedSize = UIView.layoutFittingCompressedSize
compressedSize.width = width
let previewSize = view.systemLayoutSizeFitting(
compressedSize,
withHorizontalFittingPriority: .required,
verticalFittingPriority: .fittingSizeLevel
)
return previewSize
// Set the frame of the cell, so that the layout can be updated.
var newFrame = view.frame
newFrame.size = CGSize(width: width, height: UIView.layoutFittingExpandedSize.height)
view.frame = newFrame

// Make sure the contents of the cell have the correct layout.
view.setNeedsLayout()
view.layoutIfNeeded()

// Get the size of the cell
let computedSize = view.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)

// 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.
return CGSize(width: width, height: ceil(computedSize.height))
case .fixed(let size):
return size
case .intrinsic:
Expand Down

0 comments on commit 3589a30

Please # to comment.