I'm using this Lottie library to render a Lottie animation in SwiftUI.
This is my Lottie file, as you can see it is flush against the left margin (left aligned):
Now I try to load this in my app as follows:
VStack(alignment: .leading, spacing: .zero) {
LottieView(animation: .named("loading-animation"))
.playing(loopMode: .loop)
.frame(height: 40)
}
As you can see here below, the Lottie animation isn't right aligned as I wish and is placed somewhere at the center.
Not sure why the above code doesn't work as is to align the Lottie animation to the left as if I just use another view with this code (A rectangle for example,) it will be left aligned as expected.
How can I make a Lottie animation view align to the left in SwiftUI?


The
alignmenton theVStackis used to determine how multiple views inside theVStackshould be aligned to each other, if they have different widths. When theVStackonly contains one view, it has no effect. The same is true of thespacing.I am guessing that the problem is because the content of the
VStack(the Lottie animation) is not filling the width of the screen. So theVStackand its contents are shown horizontally centered. If you add a border to theVStackthen you will be able to see exactly what width it has.To fix, you need to expand the view to fill the width by setting
maxWidth: .infinity. At the same time, you can decide how you want it aligned within the wider frame. You don't even need theVStackif it does not have any other content:EDIT Thanks for making the animation available. I was able to reproduce the problem. Applying
.scaledTofit()before the.framemodifiers gets it working for me:By examining the JSON file, we can see that the animation has a default size of 320x80. So the code here is maintaining the same aspect ratio but scaling (shrinking) it to fit the height of 40pt.
If you want to stretch the animation and make it wider then this can be done by applying another
scaleEffectto the scaled-down version. To use the full width of the screen, aGeometryReadercan be used to measure the available space: