3

我有一个新项目,可以使用 iOS 5+ 功能,所以我选择同时使用AutoLayoutUIViewControllerchildControllers功能。

我要创建的屏幕很简单: 1. 顶部区域有很多“复杂”控件。2. 底部区域是一个表格视图,用于可视化结果。

我想要的是

我希望控件区域占据所有必要的位置(即我给 Interface Builder 的位置)。TableView 将根据需要缩小。

问题

从 XIB 加载时,UIViewController.view帧的大小始终为 0x568。我预期为 0x200(如果 200 是我在 Interface Builder 中配置的大小)。

什么有效:与AutoLayout

我用我所有的元素创建了一个 XIB。我在“根视图”上做了插座,我做了插座和所有视图容器。

然后在我的 viewDidLoad 方法中,我添加了容器并配置了约束。

-(void) setupConstraints
{
    NSMutableArray *constraints = [NSMutableArray arrayWithCapacity:1];

    // Views dictionary.
    UIView *topView = self.topView;
    UIView *table   = self.tableTracks;
    NSDictionary *views = NSDictionaryOfVariableBindings(topView, table);

    // Views metrics.
    NSNumber *topViewHeight = [NSNumber numberWithFloat:self.topView.frame.size.height];
    NSDictionary *metrics = NSDictionaryOfVariableBindings(topViewHeight);

    // Pin the topView to the top edge of the container.
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topView(==topViewHeight)][table]|" options:0 metrics:metrics views:views]];

    // Pin the topView edges to both sides of the container.
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[topView]|" options:0 metrics:nil views:views]];

    // Pin the table edges to both sides of the container.
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[table]|" options:0 metrics:nil views:views]];

    [self.view addConstraints:constraints];
}

有了这个,我可以topView使用 Interface Builder 简单地调整视图的大小,然后TableView根据需要调整视图的大小(没有压缩阻力,我不在乎我们看不到表格)。

什么不工作:使用AutoLayoutChildControllers

然后为了简单起见,我选择使用ChildControllers(执行自定义 UIFont 设置需要很多插座,太糟糕了 XCode 还不能处理它们!)。我做了以下修改:

  1. 创建 HomeTopViewController 类 + XIB。创建 HomeTableViewController 类 + XIB。
  2. 将所有视图从原点复制到正确的 XIB。添加 UIViewController 引用 + 连接插座。
  3. 的根容器HomeTopViewController配置为200px高度。
  4. 将我的容器连接到 view我孩子控制器的插座。

然后我将我的设置代码更新为以下内容:

-(void) _addChildViewControllersAndSetupConstraints {
    // Get required metrics variables
    NSNumber *topViewHeight = [NSNumber numberWithFloat:self.topViewController.view.frame.size.height];

    CFShow(self.topViewController.view);
    // log is : <UIView: 0xa209c20; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0xa2097e0>>
    NSLog(@"self.topViewController.view.frame.size.height = %f", self.topViewController.view.frame.size.height);


    // Informs controllers the ADD operation started
    [self addChildViewController:self.topViewController];
    [self addChildViewController:self.tableViewController];

    // Add view to the view's hierarchy
    self.topViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
    self.tableViewController.view.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview:self.topViewController.view];
    [self.view addSubview:self.tableViewController.view];


    // Setup constraints
    NSMutableArray *constraints = [NSMutableArray arrayWithCapacity:1];

    // Views dictionary
    UIView *topView = self.topViewController.view;
    UIView *table   = self.tableViewController.view;

    NSDictionary *views = NSDictionaryOfVariableBindings(topView, table);

    // Views metrics dictionary
    NSDictionary *metrics = NSDictionaryOfVariableBindings(topViewHeight);

    // Pin the topView to the top edge of the container.
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topView(==topViewHeight)][table]|" options:0 metrics:metrics views:views]];

    // Pin the topView edges to both sides of the container.
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[topView]|" options:0 metrics:nil views:views]];

    // Pin the table edges to both sides of the container.
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[table]|" options:0 metrics:nil views:views]];

    // Adds constraints
    [self.view addConstraints:constraints];


    // Informs controllers the ADD operation ended
    [self.topViewController didMoveToParentViewController:self];
    [self.tableViewController didMoveToParentViewController:self];
}

问题

无论我如何调整 UIView 容器的大小,HomeTopViewController当我期望 200px 时,它CFShow(self.topViewController.view);总是会给我。frame = (0 0; 320 568)

我确实将布局大小配置为“自由格式”或“无”。

我想尽可能避免的

我讨厌必须创建其他插座,然后ViewHomeTopViewController初始HomeTableViewController帧/高度存储到 UIViewController 属性中。

问题

  1. 在加载时,所有 controller.view.frame 属性是否都在屏幕上调整大小?(无论是 3,5 还是 4 英寸)
  2. 我怎样才能解决这个问题而不在某处硬编码大小//创建额外的出口?
4

1 回答 1

0

如果您使用故事板,这会更容易。您可以将容器视图添加到主控制器的视图中,并根据需要调整它们的大小,并且您会自动将视图控制器嵌入到这些容器视图中,并设置为正确的大小。这些容器视图(在对象列表中的常规 UIView 旁边)只能从情节提要中获得,而不是 xibs。这几乎不需要任何代码,而且您可能不必手动添加任何约束。

于 2013-01-03T20:45:39.600 回答