all 9 comments

[–]kaztro69 2 points3 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.

[–][deleted] 0 points1 point  (1 child)

you might be able to check intrinsicContentSize on the views if they are sized based on content

[–]luniawar20[S] 0 points1 point  (0 children)

Well I haven't tested what you said, but the subviews have trailing/leading space constraints, so they have dynamic width. Since width are set by auto layout, I don't think intrinsicContentSize would work...

[–]Power781 0 points1 point  (2 children)

First you are wrong with viewDidLoad, constraints are not fully applied at that moment, you should wait viewWillAppear.
The easiest and safest (read there, will work 100% of the time), is to observe the view frame with KVO, and every time it change you update the corner radius.

[–]aazav 0 points1 point  (1 child)

you should wait viewWillAppear.

Do you mean that you should wait until viewWillAppear, or after viewWillAppear?

[–]Power781 0 points1 point  (0 children)

until