Reputation: 625
In my code I have a viewWillAppear call which is like the one below designed to move certain objects out of the way.
override func viewWillAppear(_ animated: Bool) {
playButton.center.x -= view.bounds.width
howToButton.center.x -= view.bounds.width
imageView1.center.x = view.bounds.width - (imageView1.frame.width / 2)
imageView2.center.x = view.bounds.width - (imageView2.frame.width / 2)
imageView1.center.x += imageView1.frame.width
imageView2.center.x += imageView2.frame.width
introductoryLabel.alpha = 0
}
This is followed by a viewDidAppear call which animates these objects into scene. My problem is that the viewDidAppear does the animations (which basically reverses what's in viewWillAppear) as if viewWillAppear hadn't been called. Interestingly this is only a problem when I apply constraints in the storyboard. How can I keep the constraints but allow viewDidAppear to do its thing?
Any help would be appreciated!
Upvotes: 0
Views: 93
Reputation: 256
Views from Xib or Storyboard are still repositioning in willWillApear
,willDidApear
methods so you cannont change the view's frame. If you really want to do this, change them after some delay,eg. 0.25s, or you can change them in viewDidLayoutSubViews
method.(Not recommended,called multiple times).Furthermore,you can animate the views by changing view.transform
or change the constraints.
Upvotes: 0
Reputation: 535557
Interestingly this is only a problem when I apply constraints in the storyboard
This is not at all surprising. If you have applied constraints, you cannot then change the center
of your views. Well, you can, but it is useless to do so, because the constraints will then proceed to reposition them according to the constraints. That, after all, is the purpose of constraints!
So, basically, there are two ways to position things: using their frame
or center
, on the one hand, and using constraints, on the other hand. Don't try to mix them together. Use one or the other.
Upvotes: 1
Reputation: 817
I'm not sure this will help but you should call super.viewDidAppear(animated) like this:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated) // This is what you may be missing
playButton.center.x -= view.bounds.width
howToButton.center.x -= view.bounds.width
imageView1.center.x = view.bounds.width - (imageView1.frame.width / 2)
imageView2.center.x = view.bounds.width - (imageView2.frame.width / 2)
imageView1.center.x += imageView1.frame.width
imageView2.center.x += imageView2.frame.width
introductoryLabel.alpha = 0
}
Upvotes: 0