I've been searching but can't find an answer to this issue I'm having. It seems fairly fundamental, so hopefully someone can explain, or point me towards a previous post.
When adding a viewcontroller's view as a subview of another viewcontroller, I find that the subview's height property goes to zero upon rotation. The width tends to increase as well.
For example, with NSDChildViewController's view set to 50x100 in the xib file...
@implementation ParentViewController
-(void)viewDidLoad
{
[super viewDidLoad];
mChild = [[ChildViewController alloc] initWithNibName:nil
bundle:nil];
[self.view addSubview:mChild.view];
}
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[mChild printFrame];
}
@end
@implementation ChildViewController
-(void)printFrame
{
NSLog(@"%s %@",__FUNCTION__, NSStringFromCGRect(self.view.frame));
}
@end
The logging for respective orientations portrait->landscape->portrait is as follows:
-[ChildViewController printFrame] {{0, 0}, {50, 100}}
-[ChildViewController printFrame] {{0, 0}, {298, 0}}
-[ChildViewController printFrame] {{0, 0}, {50, 248}}
Why does this happen and how can I prevent it? Presently it's causing me trouble whilst trying to programmatically layout viewcontrollers as subviews. The only solution I've found so far is to force the size property back to 50x100 using something like this...
-(void)forceSize
{
CGRect frame = self.view.frame;
frame.size.width=50;
frame.size.height=100;
self.view.frame=frame;
}
but the above seems ludicrous. Any help appreciated.