iOS 新视图控制器的 addSubview 不起作用

iOS addSubview of a new view controller does not work

我正在尝试做这样的事情 -

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

它只是给我一个没有任何内容的白屏。如果我将新的视图控制器推送到导航堆栈上,它会正确显示控制器。知道我可能遗漏了什么吗?

您应该在 viewDidLayoutSubviews 上设置 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);
}