新视图控制器的iOS addSubview不工作
我正试着做这样的事情-新视图控制器的iOS addSubview不工作,ios,uiview,uiviewcontroller,uinavigationcontroller,Ios,Uiview,Uiviewcontroller,Uinavigationcontroller,我正试着做这样的事情- - (void)viewDidLoad { [super viewDidLoad]; ThingViewController *thingViewController = [self thingControllerForCard:self.card]; thingViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.he
- (void)viewDidLoad {
[super viewDidLoad];
ThingViewController *thingViewController = [self thingControllerForCard:self.card];
thingViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
UIView *view = [thingViewController view];
[self.view addSubview:view];
}
它只是给了我一个没有任何东西的白色屏幕。如果我将新的视图控制器推到导航堆栈上,它会正确显示控制器。你知道我可能缺少什么吗?你应该在
viewdilayoutsubviews
上设置thingViewController框架。
viewDidLoad
此时未正确设置帧:
- (void)viewDidLoad {
[super viewDidLoad];
ThingViewController *thingViewController = [self thingControllerForCard:self.card];
UIView *view = [thingViewController view];
[self.view addSubview:view];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
thingViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
这是正确的,但viewDidLayoutSubviews确实是视图控制器布局子视图的正确位置。例如,如果视图的框架发生更改,将调用viewDidLayoutSubviews。仅当视图添加到视图层次结构时,才会调用ViewWillAppeal。更新,谢谢@rschmidt。有趣的是,我喜欢使用viewDidLayoutSubviews进行帧设置,不知道为什么我忘记了。你不应该只将一个VC的视图添加到另一个视图控制器-你需要告诉两个VC他们都有一个子/父视图控制器。Apple的View Controller编程指南中介绍了相关的方法调用。