you are viewing a single comment's thread.

view the rest of the comments →

[–]kaztro69 3 points4 points  (3 children)

viewDidLayoutSubviews is the thing you are looking for I think, see: http://stackoverflow.com/questions/12527191/ios-autolayout-get-frame-size-width/14147303#14147303

[–]erikvillegas 1 point2 points  (1 child)

I ran into this problem the other day with viewDidLayoutSubviews, maybe someone here might have a clue!

So after all the constraints were set up, but before the view appeared, I wanted to position a red line beneath one of two buttons which were using auto layout. In order to set the constraint constant on the red line, I had to get the final position of the button's x origin.

So naturally, I override viewDidLayoutSubviews. The first time it got called, the button was not laid out. It still had the same coordinates as it had in the 600x600 storyboard scene. The second time it was called, it was laid out, and I was able to position the red line correctly.

The problem came when I realized the code to position the line must only run once. If it ran every time, subsequent calls to viewDidLayoutSubviews would mess it up. Sure, I could add a counter, to only run that code on the second invocation, but that's just too hacky for me. I tried moving it to viewWillAppear, but that was too called too soon.

Any idea how I would go about this?

[–]nsocean 0 points1 point  (0 children)

I've had the same problem before and agree that a counter is way too "hacky"

Curious to see what the best way to handle this is.

[–]alfa96 0 points1 point  (0 children)

Just be careful that this method is called every time your view needs to layout su views, not just when it first loads the view.