|
In my app, I have a bunch of images for displaying letters. The images comes in 2 different sizes, depending on how many letters are in the image. The problem: I can't get HStack to resize the images proportionally so that images of two different widths have the same height. The UIStackView is straightforward to setup:
The problematic HStack:
Things I've tried:
Idea I just can't get to work:
Any ideas on how I can get this HStack to play along properly? I'd really like to stick with a pure SwiftUI solution if I can. |
|
I think of ForEach as a small factory. It creates a bunch of views. In your case, it's creating Images containing graphemes. Are they coming out of your factory in different sized boxes? In your code you set the frame on the HStack. This is setting the size of a railway boxcar. But the grapheme boxes inside the boxcar are all different sizes. Did you consider putting frames on each grapheme view?
You may be interested in SwiftUI programmer CodeSlicing's library named PureSwiftUI. See: Code Slicing His library addresses many SwiftUI holes, or tries to make portions easier to use. In particular, he provides View extentions for components that don't expand to fill all available space by default. His example:
His library may help you? Your mileage may vary. |
|
Thanks for the reply and the info about PureSwiftUI. Unfortunately, applying frames to each of the Images directly doesn't work. I can see in the preview that the frames are being modified (e.g. maxHeight: 100 makes them all uniform height), but that doesn't change the resizing of the image itself -- it just affects the empty space around the image. maxWidth: .infinity doesn't change the width of the frames -- they all stay an equal width with or without that modifier. |
|
|
|
Here's the test project on GitHub if you want to play around with both the SwiftUI and UIKit versions: The project launches into the view you see in the first post. The "a" and "bb" at the bottom are buttons that add more images to display, which helps for testing how things will scale when the words are longer. If you just want the images: |
|
|
|
I could use a Lazy stack, but it's not clicking with me how that would help solve my problem. Any suggestions on an approach I could take? |
|
After reading up more on HStack, it really doesn't seem like it's possibility in the current incarnation of SwiftUI. However, something tickled the back of my brain, and I remembered that something does allow for variable view width: LazyVGrid. With that in mind, I was able to make something work! It relies on dynamically generated columns and calculating view size based on contents and available space. It's not the prettiest, but hopefully this helps someone in the future :)
|
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.