Overview
I have the following view hierarchy that mixes SwiftUI and UIKit:
AccordionView
└─ VStack
├─ Text
├─ Button
└─ UIViewRepresentable
└─ UIStackView
├─ UILabel
└─ UILabel
When tapping the button, the UIViewRepresentable hides and shows its content. This all works as expected.
However, in certain circumstances the view's sizing is rendered with the correct size, but the text can often render incorrectly, despite the frame seemingly looking as though it has enough room to render the text.
More info
Below you can see the UILabel has the correct frame height (the light grey background and coloured borders) but the text is rendered as though it has infinite width along one line.
There's a few configurations of my view hierarchy that seem to have this effect.
I've added a playground to the bottom of this post of various configurations to show what does and doesn't work, just copy and paste to see for yourself...
It seems of the ones that don't work, there's a couple of reasons why that may be:
-
HostedViewandTextViewContainerdo not do the following (I think we only need to do one of these things for auto layout/stack views to work effectively):a) implement an intrinsic content size
b) return a 'good' size for
systemLayoutSizeFitting(). -
UIHostingControllershouldn't use intrinsic size (although I'm sure it should) -
Something related to setting
setContentCompressionResistancePriority()orsetContentHuggingPriority()but having played about with this it doesn't seem relevant here...
I've played around with everything I can think of here but can't find a solution that works for all, although I'm 99% sure it's one or all of the points above.
If there are any UIKit gurus out there that can help that would be great! Ive already spent so much time on this 🫨
Playground