Ios – view controller frame size in child controller

iosobjective cuiviewcontroller

I am implementing UIViewcontroller containment. In the example below I set the frame size of the childcontrollers in the rootcontroller. The child view appears as the size I have set, however when I check on its bounds within container1 it reports a different size to the size I set.

Rootcontroller (container)

- (void)viewDidLoad
{
  [super viewDidLoad];


self.containA = [[Container1 alloc]init];
self.containB = [[Container2 alloc]init];

 self.containA.view.frame = CGRectMake(50, 50,50, 50);
 self.containB.view.frame = CGRectMake(50, 50, 300, 300);

   [self addChildViewController:self.containA];
   [self addChildViewController:self.containB];

  [self.view addSubview:self.containA.view];

Container1

-(void)viewDidLoad {
    [super viewDidLoad];




UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
//[self.view addSubview:view];
self.view = view;
self.view.backgroundColor = [UIColor redColor];
[view release];
 NSLog(@"view dims %f %f",self.view.bounds.size.width,self.view.bounds.size.height);
}

Console output from container 1
view dims 768.000000 1004.000000

I thought the issue was related to setting the view frame to UIScreen main screen]applicationFrame .So I removed all of this code so the uiview is created automatically. The issue still remains..

Best Answer

View frames are not actually useable in viewDidLoad; you should move all of your geometry-manipulating code into viewWillAppear. The system will clobber any changes you set in viewDidLoad between there and when it's about tom come on screen.

Related Topic