Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add subView to anotherSubView with center alignment in iOS swift.

Tags:

xcode

ios

swift

I'm using lottie library for animation. I loaded lottieFile as subView to anotherSubView but it's not aligned in center. I tried using center attributes as below:

@IBOutlet weak var viewOn: UIView!

let animationView = LOTAnimationView(name: "restless_gift_ii") {
animationView.loopAnimation = true
animationView.contentMode = .scaleToFill
animationView.animationSpeed = 1
animationView.backgroundColor = UIColor.black
animationView.frame.size = viewOn.frame.size
animationView.center.x = viewOn.center.x
animationView.center.y = viewOn.center.y
viewOn.addSubview(animationView) }

Result not aligned

like image 543
user3853770 Avatar asked Dec 06 '22 12:12

user3853770


1 Answers

You can use auto layout programmatically to center align your animation view into it’s super view.

Here, I have added two ways to add animationView center align and also added comments for understanding.

if let animationView = LOTAnimationView(name: "4_bar_loop") {

        animationView.loopAnimation = true
        animationView.contentMode = .scaleToFill
        animationView.animationSpeed = 1
        animationView.backgroundColor = UIColor.black

        self.viewOn.addSubview(animationView)

        animationView.translatesAutoresizingMaskIntoConstraints = false

        // Apply these constrains if you want animation size should be same as super view.
        self.viewOn.addConstraint(NSLayoutConstraint(item: animationView, attribute: .leading, relatedBy: .equal, toItem: self.viewOn, attribute: .leading, multiplier: 1.0, constant: 1))
        self.viewOn.addConstraint(NSLayoutConstraint(item: animationView, attribute: .trailing, relatedBy: .equal, toItem: self.viewOn, attribute: .trailing, multiplier: 1.0, constant: 1))
        self.viewOn.addConstraint(NSLayoutConstraint(item: animationView, attribute: .top, relatedBy: .equal, toItem: self.viewOn, attribute:.top, multiplier: 1.0, constant: 1))
        self.viewOn.addConstraint(NSLayoutConstraint(item: animationView, attribute: .bottom, relatedBy: .equal, toItem: self.viewOn, attribute: .bottom, multiplier: 1.0, constant: 1))


        // Apply these constraint if you want animationView with fixed height and width and center of super view.
//            self.viewOn.addConstraint(NSLayoutConstraint(item: animationView, attribute: .centerX, relatedBy: .equal, toItem: self.viewOn, attribute: .centerX, multiplier: 1.0, constant: 1))
//            self.viewOn.addConstraint(NSLayoutConstraint(item: animationView, attribute: .centerY, relatedBy: .equal, toItem: self.viewOn, attribute: .centerY, multiplier: 1.0, constant: 1))
//            animationView.addConstraint(NSLayoutConstraint(item: animationView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
//            animationView.addConstraint(NSLayoutConstraint(item: animationView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        }
like image 73
Sagar Chauhan Avatar answered May 17 '23 14:05

Sagar Chauhan