在子控制器中查看控制器框架大小

我正在实施UIViewcontroller遏制。 在下面的例子中,我设置了根控制器中childcontrollers的框架大小。 子视图显示为我设置的大小,但是当我检查container1中的边界时,它将报告与我设置的大小不同的大小。

根控制器(容器)

- (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); } 

控制台输出从容器1视图dims 768.000000 1004.000000

我认为这个问题是相关的设置视图框架到UIScreen主屏幕] applicationFrame。所以我删除了所有这些代码,所以uiview是自动创build的。 问题仍然存在

视图框架在viewDidLoad中实际上是不可用的; 你应该将所有的几何操作代码移动到viewWillAppear中。 系统将会在你的屏幕上closurestom的时候,摧毁你在viewDidLoad中设置的所有更改。