ios - 横向时addChildViewController不起作用

标签 ios objective-c ios7

我正在使用容器 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 ,则会发生这种情况:

enter image description here

有什么线索吗?

最佳答案

只需要设置框架。打开时的 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/

相关文章:

ios - getControlPointAtIndex 参数类型

objective-c - Ipad Split View,是否可以调整 Split View的根 Controller 的大小?

objective-c - 将对象添加到数组后,tableview 不会更新

c++ - 在 XCode 4.6.2 上使用 cryptopp 静态库在 iOS 应用程序中抛出异常

ios - 为什么我的普通 UITableView 部分页脚 float 在 iOS 7 中输入的附件 View 上方?

iOS 7 搜索栏 - 取消按钮不起作用

ios - Swift 4.2 已经在文档中了吗?

ios - 更改状态栏颜色 Objective-C

iPhone 数据使用跟踪/监控

jquery - iOS7 中 jQuery Mobile 滑动事件的不稳定行为