我有故事板:
如您所见,我有 MenuViewController:UIViewController 与容器。容器为
MenuTableViewController:UITableViewController
使用 MenuTableView:UITableView 。
我想做的是更改容器(MenuTableViewController)的大小,并向mainVC添加另一个Child。
我将其添加到MenuViewController中:
-(void)viewDidAppear:(BOOL)animated {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
MenuTableViewController* secondChildVC = [sb instantiateViewControllerWithIdentifier:@"MenuTableViewController"];
[secondChildVC.view setBackgroundColor:[UIColor redColor]];
[self addChildViewController:secondChildVC];
NSLog(@"MenuTableViewController.view info: %@",secondChildVC.view);
NSLog(@"secondChildVC views: %@",[secondChildVC.view subviews]);
MenuTableViewController *firstChildVC = [self.childViewControllers objectAtIndex:0];
[firstChildVC.view setFrame:CGRectMake(160, 0, 160, 504)];
[firstChildVC.view setBackgroundColor:[UIColor greenColor]];
NSLog(@"firstChildVC views: %@",[firstChildVC.view subviews]);
}
这是我的日志:
MenuTableViewController.view info: <MenuTableView: 0x8910e00; baseClass = UITableView; frame = (0 20; 320 548); clipsToBounds = YES; opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0x761eca0>; layer = <CALayer: 0x76202b0>; contentOffset: {0, 0}>
2013-05-03 17:20:10.796 TestStories[41780:c07] secondChildVC views: (
"<UIImageView: 0x76205e0; frame = (0 541; 320 7); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x7620680>>",
"<UIImageView: 0x7620740; frame = (313 517; 7 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x76207e0>>"
)
2013-05-03 17:20:10.798 TestStories[41780:c07] firstChildVC views: (
"<UITableViewCell: 0x7143110; frame = (0 320; 160 320); autoresize = W; layer = <CALayer: 0x713cc10>>",
"<UITableViewCell: 0x7140b90; frame = (0 0; 160 320); autoresize = W; layer = <CALayer: 0x7140ce0>>",
"<UIImageView: 0x7135000; frame = (0 497; 320 7); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x71351b0>>",
"<UIImageView: 0x7135270; frame = (153 517; 7 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x7135310>>"
)
为什么我的secondChildVC不包含TableViewCells?不知道。
您可以看到,我使用红色和绿色作为背景(红色表示secondChildCV)。但是可能永远不会出现。这是屏幕截图:
最佳答案
我认为您做错了几件事。子视图控制器的大小将与容器视图的大小相同,因此您应该为容器视图添加一个IBOutlet并更改其大小,而不是更改子视图控制器的大小。
对于第二个子级,将其添加为子级,但不要将其视图添加到MenuViewController的视图中。您也应该在将其添加为子级之后立即调用didMoveToParentViewController。因此,您需要执行以下操作:
MenuTableViewController* secondChildVC = [self.storyboard instantiateViewControllerWithIdentifier:@"MenuTableViewController"];
[secondChildVC.view setBackgroundColor:[UIColor redColor]];
[self addChildViewController:secondChildVC];
[secondChildVC didMoveToParentViewController:self];
secondChildVC.view.frame = CGRectMake(0,0,160,504);
[self.view addSubview:secondChildVC.view];
还要注意,您可以使用self.storyboard更简单地获得对故事板的引用
关于ios - 如何以编程方式添加ChildVC?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16362427/