2

I want to use the awesome MultiSegmentPicker written by Yonat Sharon in my SwiftUI View.

https://github.com/yonat/MultiSelectSegmentedControl

However, I don't fully understand the interaction between the UIViewRepresentable View and my SwiftUI View. How do I get the host view controller to shrink its height down to the size of the segmented control?

Here's the debugger view of the demo page - notice the blue area around the top bar: enter image description here

The demo code doesn't give a lot of insight into the issue, it's just a call to the UIViewRepresentable view. I've simplified it to just one example here:

   struct MultiSegmentPickerX: View {

    @State private var selectedSegmentIndexes: IndexSet = []

    var body: some View {
        VStack(alignment: .center) {
            Spacer()
            MultiSegmentPicker(
                selectedSegmentIndexes: $selectedSegmentIndexes,
                items: ["First", "Second", "Third", "Done"]
            )
        }
    }
}

enter image description here

Notice I have a VStack with a Spacer() before the control.

The desired behavior for this example would be that the bar with "First", "Second", etc. would be snug against the bottom of the screen. Instead the Host Controller holds onto all that space...

Do I need to use Geometry reader to solve this issue and shrink the height down. Or do I have to adjust something in the UIViewRepresentable View?

Any insights into bridging UIKit and SwiftUI are always appreciated... Is this an easy fix anyone?

These did not solve my issue: UIViewRepresentable content not updating

How do I make SwiftUI UIViewRepresentable view hug its content?

How do I size a UITextView to its content?

Mozahler
  • 3,643
  • 6
  • 24
  • 42

1 Answers1

3

Cannot test it now, just by thoughts, try with fixed size as below

MultiSegmentPicker(
    selectedSegmentIndexes: $selectedSegmentIndexes,
    items: ["First", "Second", "Third", "Done"]
).fixedSize()       // << here !!
Asperi
  • 123,447
  • 8
  • 131
  • 245
  • 1
    For me this didn't work without fixing the `intrinsicContentSize` of the original UIKit control. I pushed the fix in version 2.3.3. – Yonat Apr 17 '20 at 07:11