I've added the "Lottie iOS" package to my SwiftUI project and successfully managed to display lotties.
I've created a custom view with a lottie inside and now I want to embed it inside a List section.
This is my code for the custom view:
struct ParcelDetailHeader: View {
var body: some View {
VStack(alignment:.center) {
LottieView(loopMode: .loop)
.frame(height:200)
.scaleEffect(0.40)
}
.frame(maxWidth: .infinity)
.frame(height:200)
.background(Color("my_lottie_yellow"))
}
}
And this is my code for the List (which is embedded in another view, inside a NavigationStack) in which I'd like to put it:
struct ParcelDetail: View {
let item: ParcelItem
@EnvironmentObject var order: Parcel
var body: some View {
List {
Section {
HStack{
Text(item.tracknumber)
Spacer()
Image(systemName: "shippingbox.fill")
}
ParcelDetailHeader()
.listRowInsets(EdgeInsets())
Text(item.description)
.multilineTextAlignment(.trailing)
} header:{
Text("")
}
}
.navigationTitle(item.name)
.navigationBarTitleDisplayMode(.automatic)
}
}
And this is the code for the "LottieView.swift" needed in order to play the Lottie animation:
import Lottie
import SwiftUI
struct LottieView: UIViewRepresentable {
let loopMode: LottieLoopMode
func updateUIView(_ uiView: UIViewType, context: Context) {
}
func makeUIView(context: Context) -> Lottie.LottieAnimationView {
let animationView = LottieAnimationView(name: "smile_lottie")
animationView.play()
animationView.loopMode = loopMode
animationView.contentMode = .scaleAspectFit
animationView.animationSpeed = 0.7
return animationView
}
}
#Preview {
LottieView(loopMode: .loop)
}
The problem is the unwanted space over the view, inside the row:
Tried to check all the .frame modifiers but I really can't understand where the problem is.
Thank you for posting some more of the code.
I have been trying to reproduce the problem. Here is my best effort at a minimal reproducible example. It uses improvised (dummy) implementations of
ParcelItem
(which was missing from the code you posted) andLottieView
(because I don't have the third-party library):I don't think the smiling image/animation is the problem, because its height is constrained to 200 and the background is yellow. So I think you need to focus on the content above the image.
The only way I could get it to show a large blank space above the image is if I use "\n\n\n\n\n\n" as
item.tracknumber
. This then appears as follows:This screenshot is different to your screenshot in a couple of ways:
item.description
is displayed).So here are some suggestions:
Text(item.tracknumber)
withText("item.tracknumber")
, to see whether this is the cause of the extra height.View
that containsParcelDetail
. What is the implementation of this container?