我正在使用容器视图控制器在3秒后将子视图添加到当前视图控制器:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor greenColor];
[self performSelector:@selector(open) withObject:nil afterDelay:3.0];
}
-(void)open{
ViewController2 *test = [[ViewController2 alloc] init];
test.view.backgroundColor = [UIColor redColor];
[self addChildViewController:test];
[self.view addSubview:test.view];
}
ViewController2是一个简单的视图,在init上只有这个:
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
如果我以纵向打开第二个视图并旋转到横向,红色会填满屏幕,但如果我从横向打开第二个视图,则会发生以下情况:
任何线索?
答案 0 :(得分:3)
只需要设置框架。打开时的视图界面将采用您投影的视图的大小。
-(void)open{
ViewController2 *test = [[ViewController2 alloc] init];
test.view.backgroundColor = [UIColor redColor];
test.view.frame = self.view.bounds;
[self addChildViewController:test];
[self.view addSubview:test.view];
}
作为建议:开始学习自动布局!
答案 1 :(得分:0)
这是一个适合您的解决方案。
-(void)open{
test = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
test.view.backgroundColor = [UIColor redColor];
test.view.frame = self.view.bounds;
[self addChildViewController:test];
[self.view addSubview:test.view];
}