I have a view controller loaded from a nib where I have a scroll view defined with a height of 346 pixels. However when I do:
int h = scrollView.frame.size.height;
NSLog(@"%d",h);
in viewDidLoad I get 375.
I n开发者_运维百科eed the proper height in viewDidLoad (or another similar method) so that I can programatically set the contents of the scroll view depending on the height (don't want it to 'overflow' vertically, only horizontally, its paged).
The scroll view has every single autoresizing line turned on. (all bright red). Strangely, when the view loads I can see that the scroll view is (the proper) 346 pixels high not 375, so it must get changed to the proper value somewhere between viewDidLoad and displaying the view.
I'm at a dead end here and any way to get the proper size of a view would be immensely helpful. Thanks in advance, and if you need any more info please just comment.
edit: The way I solved it was by putting a timer for 0.1 seconds into viewDidLoad, and when that timer is called I did the work I needed to do. This is a bit of a hack though so any other advice would be much appreciated.
The correct place to perform additional layout-type calculations is generally in viewWillLayoutSubviews
or viewDidLayoutSubviews
. Docs here. Also this from the section on "Handling View Rotations":
The
viewWillLayoutSubviews
method is also called after the view is resized and positioned by its parent. If a view controller is not visible when an orientation change occurs, then the rotation methods are never called. However, theviewWillLayoutSubviews
method is called when the view becomes visible.
Take a look at this link.
You subview hasn't been placed and layouted within the superview. Get your view's size in viewDidAppear:
for example.
精彩评论