4

I have UIView that has auto layout constraints. I create it with frame CGRectZero. So I don't the real size. It will depend on superview.

But I want to know when my view get a frame that I can use for my calculation.

Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277

1 Answers1

8

So you shouldn't really need to know anything about the frame. Although if you did need to know, you could just implement -layoutSubviews in your view, or -viewDidLayoutSubviews at the view controller level.

Here after calling super the frames should be set to what AutoLayout resolved them to be.

Also good practise to take on board is #7 of the following post: Forget frame.

I also highly recommend reading about the AutoLayout process here.

Daniel
  • 23,129
  • 12
  • 109
  • 154
  • 1
    ok I have a view A that contains another view B. And view B has image. everything is based on auto-layout. Then seems I need to subcalsing UIView B to perform -layouts subviews? Because I am interested in image that is part of view B. and I need to add top corner radius to image view via CALayer. – Matrosov Oleksandr Feb 11 '15 at 23:10
  • 1
    If all you need to do is add a corner radius to your image view, then you don't need to worry about frames, just do that in view A's awakeFromNib, or in it's initWithFrame:. If you've got a complete storyboard setup and none of your views are custom UIView subclasses, then you could just add an outlet from view B to your view controller and inside of viewDidLoad set the corner radius. – Daniel Feb 11 '15 at 23:13
  • 1
    Although how are you going to deal with layer changes if you forget frames? What about the times when you are dealing with `CGRect` values, not necessarily directly connected to a view? – funct7 Jun 14 '16 at 12:21
  • generally you might not need to know the frame, but in my case, I have a template image which I want to set a background color for, and want to calculate the radius so it masks as a circle behind the circular image. Thus, I need to know the frame for the image view and hardcoding it feels dirty. However, layoutSubviews does not seem to work for me. (it is in a UITableViewCell subclass, and the imageview is a circular accessory view) – Andrew Kinnie May 30 '17 at 01:00