我正在使用容器 View Controller 在 3 秒后将 subview 添加到我当前的 View Controller :
- (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 是一个简单的 View ,在初始化时只有这个:
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
事情是这样的,如果我以纵向打开第二个 View 并旋转到横向,红色会填满屏幕,但是如果我从横向打开第二个 View ,则会发生这种情况:
有什么线索吗?
最佳答案
只需要设置框架。打开时的 View 界面采用您投影的 View 的大小。
-(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];
}
作为一个建议:开始学习自动布局!
关于ios - 横向时addChildViewController不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19726529/