1

我正在使用容器视图控制器在 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 是一个简单的视图,在初始化时只有这个:

self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

事情是这样的,如果我以纵向打开第二个视图并旋转到横向,红色会填满屏幕,但是如果我从横向打开第二个视图,则会发生这种情况:

在此处输入图像描述

有什么线索吗?

4

2 回答 2

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

作为一个建议:开始学习自动布局!

于 2013-11-01T12:21:52.410 回答
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];
 }

于 2015-05-11T09:42:41.210 回答