I have a UIView that is hooked up to my View Controller as IBOutlet. I assigned an AnimationView class instance to this property in my ViewDidLoad method and set constraints to it with animationView.snp.makeConstraints { (make) in make.width.equalTo(view).multipliedBy(0.5) }
But when I run my project, I get this error message saying "Does the constraint or its anchors reference items in different view hierarchies? That's illegal"
I have no idea why I get this error message. Can anyone explain what I am doing wrong?
And the source code in text:
import UIKit
import SnapKit
import Lottie
class ViewController: UIViewController {
@IBOutlet var myView: UIView!
override func viewDidLoad() {
myView = AnimationView()
myView.snp.makeConstraints { (make) in
make.width.equalTo(view).multipliedBy(0.5)
make.height.equalTo(view).multipliedBy(0.5)
}
}
}
Update:
How I fixed this problem was by these steps:
- Create a placeholder view in Storyboard and add it as IBOutlet to your File's owner (make sure you type
AnimationView
as its class in the far right panel). - Create an animation view programmatically and add it to its immediate super view.
- Give this animation view the same frame as the placeholder view
animationView.frame = placeholderView.frame
- Give constraints you want to the placeholder view (
placeholderView.snp.makeConstraints { (make) in ... }
)
Now you hooked up your view created programmatically to your placeholder view in Storyboard. Your programmatic view will change size with the placeholder view's size.