You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am encountering a layout issue when rendering a Markdown ordered list using the swift-markdown-ui library wrapped inside a UICollectionView cell.
The Setup:
Library: swift-markdown-ui
Architecture: SwiftUI View wrapped in a UIHostingController, embedded inside a UICollectionViewCell.
Layout: UICollectionViewCompositionalLayout with UICollectionLayoutListConfiguration.
The Issue: When the cell is first loaded, the ordered list numbering (1, 2, 3...) appears incorrectly wrapped.
However, if I scroll the view (causing the cell to go off-screen and be reused/redrawn), the layout corrects itself and looks perfect. This suggests a race condition regarding the cell sizing or layout pass on the initial render.
I am using the following markdown sample text during testing.
Makanan yang disebutkan dalam artikel ini adalah:
1. Gado-gado
2. Pempek
3. Serabi
4. Pisang goreng
5. Lupis
6. Cenil
7. Mi instan (Indomie)
Code Snippets:
The SwiftUI View (MutableMarkdownView) I am displaying the markdown text using swift-markdown-ui:
The UIKit Cell (AIChatCell) I am embedding the SwiftUI view using UIHostingController rather than UIHostingConfiguration to avoid resizing limitations I encountered previously.
class AIChatCell: UICollectionViewCell {
...
private func initMutableMarkdownView() {
let mutableMarkdownView = MutableMarkdownView(
viewModel: markdownViewModel
)
// UIHostingConfiguration has limitations with resizing. We use UIHostingController instead.
// Embed the SwiftUI view in a UIHostingController
let hostingController = UIHostingController(rootView: mutableMarkdownView)
// https://medium.com/arcush-tech/two-pitfalls-to-avoid-when-working-with-uihostingcontroller-534d1507563e
hostingController.sizingOptions = .intrinsicContentSize
if #available(iOS 16.4, *) {
hostingController.safeAreaRegions = []
} else {
// Fallback on earlier versions
}
hostingController.view.translatesAutoresizingMaskIntoConstraints = false
stackView.addArrangedSubview(hostingController.view)
self.hostingController = hostingController
}
Collection View Layout I use a standard list configuration:
When the markdown text is first shown, it becomes the following. As you can see, there is an issue with the bullet numbering
If I scroll up and down the UICollectionView, causing UICollectionView redrawing, the problem will gone
Question: Has anyone experienced similar layout glitches with UIHostingController inside a Collection View? It seems like the constraints or intrinsic content size are not being calculated correctly during the first pass. Any advice on how to force the correct layout on initialization?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I am encountering a layout issue when rendering a Markdown ordered list using the
swift-markdown-uilibrary wrapped inside aUICollectionViewcell.The Setup:
swift-markdown-uiUIHostingController, embedded inside aUICollectionViewCell.UICollectionViewCompositionalLayoutwithUICollectionLayoutListConfiguration.The Issue: When the cell is first loaded, the ordered list numbering (1, 2, 3...) appears incorrectly wrapped.
However, if I scroll the view (causing the cell to go off-screen and be reused/redrawn), the layout corrects itself and looks perfect. This suggests a race condition regarding the cell sizing or layout pass on the initial render.
I am using the following markdown sample text during testing.
Code Snippets:
When the markdown text is first shown, it becomes the following. As you can see, there is an issue with the bullet numbering
If I scroll up and down the UICollectionView, causing UICollectionView redrawing, the problem will gone
Question: Has anyone experienced similar layout glitches with UIHostingController inside a Collection View? It seems like the constraints or intrinsic content size are not being calculated correctly during the first pass. Any advice on how to force the correct layout on initialization?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions