【问题标题】:Issue with AutoLayout and ChildViewControllers (incorrect size of ChildVCs' view)AutoLayout 和 ChildViewControllers 的问题(ChildVCs 视图的大小不正确)
【发布时间】:2013-01-05 15:59:35
【问题描述】:

我在做一件相当简单的事情时遇到了一些困难,我错过了一些东西但没有看到......

我用一个非常简单的应用程序(使用 IB)重现了这个问题:

  • App 的主 ViewController 是 UINavigationController。
  • NavigationController 的根是“FirstViewController”。
  • FirstViewController 和 SecondViewController 是空的 UIViewController 子类。
    • 它们的 XIB 文件在创建类时由 XCode 生成,已启用 AutoLayout。
    • 我在 SecondViewController 的顶部和底部放置了标签(垂直空间限制 = 0)。

使用 ChildViewControllers

问题是如果我通过“ChildViewControllers”方法显示 SecondViewController,它在我的 iPhone4 上会出错:我看不到底部标签。

// In FirstViewController.m
- (IBAction)child:(id)sender {
    [self addChildViewController:self.secondVC];
    [self.view addSubview:self.secondVC.view];
    [self.secondVC didMoveToParentViewController:self];
}

使用导航控制器

如果我通过NavigationController显示“SecondViewController”,一切正常,SecondViewController显示正常。

// In FirstViewController.m
- (IBAction)push:(id)sender {
    [self.navigationController pushViewController:self.secondVC animated:YES];
}

另外,SecondViewController 通过 NavigationController 显示一次后,就会一直显示良好。

我肯定错过了什么,但是什么? :p 你有什么想法吗?

我在 Dropbox 上上传了简单的项目:https://dl.dropbox.com/u/36803737/sharebox/AutoLayoutTest.zip

谢谢!

朱利安

【问题讨论】:

标签: ios autolayout childviewcontroller


【解决方案1】:

您的保管箱链接无效,因此我无法尝试。在添加为子视图之前尝试设置 secondVC 的框架:

secondVC.view.frame = self.view.bounds;

如果你想用约束来做,我是这样做的:

- (IBAction)child:(id)sender {
    [self addChildViewController:self.secondVC];
    [self.view addSubview:self.secondVC.view];
    [self constrainViewEqual:secondVC.view];
    [self.secondVC didMoveToParentViewController:self];
}

-(void)constrainViewEqual:(UIView *) view {
    [view setTranslatesAutoresizingMaskIntoConstraints:NO];
    NSLayoutConstraint *con1 = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeCenterX relatedBy:0 toItem:view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    NSLayoutConstraint *con2 = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
    NSLayoutConstraint *con3 = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeWidth relatedBy:0 toItem:view attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
    NSLayoutConstraint *con4 = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeHeight relatedBy:0 toItem:view attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
    NSArray *constraints = @[con1,con2,con3,con4];
    [self.view addConstraints:constraints];
}

由于我经常使用约束,因此我在 UIView 的一个类别中使用了上述方法(和其他方法),以使我的代码看起来更简洁。

【讨论】:

  • @jrturton,我现在只有几个方法,主要是因为我不断更改我必须适应手头任务的少数方法,而不是添加新方法——我应该停止,并继续添加我认为广泛有用的方法。
  • 设置框架成功了!对我来说似乎不直观,但为什么不:-)也感谢您基于约束的回答!
  • 经过一天的努力,我终于找到了这篇文章,节省了我更多的时间来浪费。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
  • 2016-05-22
  • 2013-04-16
  • 2016-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多