I wonder, how and when (viewDidLoad, viewWillAppear, viewDidAppear) can I get a UIViews frame size that was autoresized to fit its partent view?
Asked
Active
Viewed 3,900 times
4 Answers
1
It's not clear from your question why you want it but I imagine it is to layout your sub views. Luckily Apple knew you would want to do that and implemented -(void)layoutSubViews
see this: When is layoutSubviews called?
-
I have (on the iPad) a webView that loads some local htmlcode to display a youtube video. the html div that holds the player needs to be the same size as the webView. but since my webview is autoresized I need to have access to its autoresized size. Yet all I get when logging the frame is the dimensions before resizing occurred. – Jakob Sep 02 '11 at 12:46
-
What bothers me with that -(void)layoutSubViews method is that I would have to subclass a UIWebView which I'd like to avoid – Jakob Sep 02 '11 at 12:58
-
Then you have a problem. Why not play the YouTube video directly so you get better control over the layout. This is a notoriously difficult problem to solve with hybrid apps. See here http://stackoverflow.com/questions/4459975/iphonehow-to-stream-and-play-youtube-video-with-in-application-using-mpmovieplay for how to do that. – Rog Sep 02 '11 at 13:59
-
I think it's pretty clear what he wants with the question, and I still couldn't find an answer too – Carlos Ricardo Aug 14 '13 at 10:23
-
subclassing a UIWebView is not such a big issue. Especially if you only want to get access to the frame. – Rog Aug 21 '13 at 10:14
-
`layoutSubviews` gets called more than once so I can't use it to create a `CGContext` with the final `frame` :/ – Brenden Nov 21 '13 at 20:42
-
That's a different question... and you should ask it. – Rog Oct 31 '14 at 17:31
0
I tried layoutSubviews
, but it gets called more than once which is a problem when I need the auto sized frame in order to create a CGContext
to draw into.
The solution I used was to expose an init
method on my custom UIView
for its parent view controller to call in its viewWillAppear
.
I wish there was a way to accomplish this all within the custom UIView
's implementation, but it eludes me (and Google and Apple's docs and SO).

Brenden
- 7,708
- 11
- 61
- 75
-6
NSLog(@"%f; %f; %f; %f;", yourView.frame.origin.x, yourView.frame.origin.y, yourView.frame.size.width, yourView.frame.size.height);
this is how you can get frame of your view

Narayanan Ramamoorthy
- 826
- 5
- 18