This is my UIViewController:
import UIKit
import Lottie
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad() {
let rocketAnimationView = AnimationView()
//Set animation
let rocketAnimation = Animation.named("my-json-rocket-animation")
rocketAnimationView.animation = rocketAnimation
rocketAnimationView.play()
}
When I build, the animation does not show--the view just remains blank. I have tried implementing the following in viewDidLoad()
, which seems to work, but my animation does not scale to my entire view:
let animation = Animation.named("my-json-rocket-animation")
animationView.animation = animation
animationView.contentMode = .scaleAspectFit
view.addSubview(animationView)
animationView.backgroundBehavior = .pauseAndRestore
animationView.translatesAutoresizingMaskIntoConstraints = false
animationView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor).isActive = true
animationView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
animationView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor).isActive = true
animationView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
animationView.setContentCompressionResistancePriority(.fittingSizeLevel, for: .horizontal)
I expected my animation to display in the view, but my screen remains blank. Do I need to create a new UIView and connect it with an IBOutlet, of class type AnimationView, and then somehow set the animation? I know this was possible by simply writing animationView.setAnimation(named: "my-json-rocket-animation")
prior to version 3.1.2, but I'm not sure if it is still possible.
I'll post my solution for anyone who has trouble with this in the future, as I think Airbnb's documentation is a little vague regarding setting an animation to a UIView. The issue was that I essentially forgot to set the view's frame size, and I had to use an IBOutlet corresponding to the UIView.
import UIKit
import Lottie
class ViewController: UIViewController {
@IBOutlet weak var lottieView: UIView!
let animationView = AnimationView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
animationView.animation = Animation.named("my-json-rocket-animation")
animationView.frame.size = lottieView.frame.size
animationView.contentMode = .scaleToFill
lottieView.addSubview(animationView)
animationView.backgroundBehavior = .pauseAndRestore
animationView.play()
}
}