3

I'm trying to wrap a custom subclass of UILabel in UIViewRepresentable to use it in SwiftUI. I'm using .sizeToFit and printing the frame, and it looks right while it's in the wrapper:

func makeUIView(context: Context) -> CustomUILabel {
    let view = CustomUILabel()
    view.customProperty = model.customProperty
    view.sizeToFit()
    print(model.latex,view.frame.size) // this prints the correct size, how to propagate?
    return view
}

but when I run this in a VStack, it draws the UIViewRepresentable with the maximum space possible.

var body: some View {
    GeometryReader{ geometry in
        VStack(spacing: 0){
            Rectangle()
                .fill(Color.red)
                .frame( height: geometry.size.height/2 - 5 + self.draggedOffset.height)
            Rectangle()
                .fill(Color.orange)
                .frame(height: 10)
            custonView(model:self.model)
            Spacer()
    }
}

Is there a way to propagate the size of the UIView to its parent, similar to how you use preference keys on a native SwiftUI view?

ricardopereira
  • 11,118
  • 5
  • 63
  • 81
chartman
  • 132
  • 2
  • 12

2 Answers2

3

It is due to use of GeometryReader

Try to use

custonView(model:self.model)
    .fixedSize()
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Didn't work for me. I'm trying to make a simple UILabel not fill the screen. – malhal Mar 25 '20 at 13:35
  • @malhal, it only means that your situation is different - post your question with your code, and then somebody here could help you. – Asperi Mar 25 '20 at 13:37
1

When using UIViewRepresentable, the greediness you describe is controlled using the contentHuggingPriority, just like it always was in UIKit.

So in your makeUIView function, you can do this:

// resist being made larger than the intrinsicContentSize
view.setContentHuggingPriority(.defaultHigh, for: .horizontal)
view.setContentHuggingPriority(.defaultHigh, for: .vertical)
Matthew
  • 1,363
  • 11
  • 21