UIView reset frames when using autostart

I ran into a problem. I am working on an application and I am using a storyboard with Autolayout enabled. Now on one of my UIViewControllers I posted a UIView (say, A). Now A has several UIViews ( UIViews , to be precise). I applied the restriction “ Horizontally and Vertically centered ” to A. The mobility inside A does not have any restrictions on them.

Now in my code, I have a method for animating subviews inside A. Inside this method, I call UIView's animation method ...

 [UIView animateWithDuration: delay: options: animations: completion:]; 

and animate one of the views inside View A. Now the problem is that these frame changes are not saved. When I modify the view controller on the current view controller and then turn it off, the frames of all subzones inside A get reset.

I looked for it and found out that the problem is due to Autolayout . I turned it off and then tried again and everything worked out well.

But what is the workaround with Autostart enabled ...?

Did I miss something...?

Interestingly, many posts on Stackoverflow have suggested that

we must animate restrictions and NOT frames when using autorun.

BUT I did not apply any restrictions to the subzones within View A. The restrictions apply only to view A.

Thanks.

+8
ios iphone autolayout animation uiview
source share
3 answers

For all the views you animate, write the following code in viewDidLoad

 myViewToAnimate.translatesAutoresizingMaskIntoConstraints = YES; 

Hope this helps

+17
source share

The reason is that Autolayout will update the views to their original position. The Autolayout system will periodically set the position and size of each view in your view hierarchy based on layout constraints.

+3
source share

Add this:

 override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() VIEWTOFIX.setTranslatesAutoresizingMaskIntoConstraints(true) } 

Inserting viewDidLayoutSubviews instead of viewDidLoad got rid of layout constraint errors for me.

0
source share

All Articles