When is it possible to know the exact size of a UIViewController
's view?
The Problem
I have a multi-line UILabel
whose frame depends of its text and the width of the parent view. Given that I need to position other views below the UILabel
, it's important to make its frame cover exactly the space of the text.
I currently calculate the size like this on viewDidLoad
:
labelSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(self.view.frame.size.width, MAX_HEIGHT)];
The problem is that the width of the parent view changes when the UIViewController
is used as a modal form sheet or a popover. If I use autoresizingMask
the frame of the UILabel is adjusted accordingly, but it no longer is an exact fit for the text.
Where can I calculate the frame of this UILabel
knowing the exact size of the UIViewController's
view?
Debugging Efforts
This is the result of printing self.view.frame when showing the UIViewController
as a modal form sheet (UIModalPresentationFormSheet
).
viewDidLoad: (0.000000;0.000000;320.000000;480.000000)
viewWillAppear: (0.000000;0.000000;768.000000;960.000000)
afterDelay: (0.000000;0.000000;540.000000;576.000000)
The code that produces the above output:
- (void)viewDidLoad {
[super viewDidLoad];
[Utils logFrame:self.view.frame tag:@"viewDidLoad"];
}
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[Utils logFrame:self.view.frame tag:@"viewWillAppear"];
[self performSelector:@selector(afterDelay) withObject:nil afterDelay:0.1];
}
- (void) afterDelay {
[Utils logFrame:self.view.frame tag:@"afterDelay"];
}