OGeek|极客世界-中国程序员成长平台

标题: 新 View Controller 的 iOS addSubview 不起作用 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:51
标题: 新 View Controller 的 iOS addSubview 不起作用

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

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

它只是给我一个白屏,里面什么都没有。如果我在导航堆栈上推送新的 View Controller ,它会正确显示 Controller 。知道我可能缺少什么吗?



Best Answer-推荐答案


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

关于新 View Controller 的 iOS addSubview 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31459063/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4