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

时间:2015-07-16 15:54:45

标签: 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.height);
  UIView *view = [thingViewController view];
  [self.view addSubview:view];
}

它只是给我一个白色屏幕,里面没有任何东西。如果我在导航堆栈上按下新的视图控制器,它会正确显示控制器。知道我可能会缺少什么吗?

1 个答案:

答案 0 :(得分:2)

您应该在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);
}