简体   繁体   English

UIViewController viewDidLoad

[英]UIViewController viewDidLoad

I've got a new project and I can work with iOS 5+ feature, so I choose to use both AutoLayout and UIViewController childControllers capabilities. 我有一个新项目,我可以使用iOS 5+功能,所以我选择使用AutoLayout和UIViewController childControllers功能。

The screen I'm trying to create is simple : 1. The top area has lot of "complex" controls. 我想要创建的屏幕很简单:1。顶部区域有很多“复杂”控件。 2. The bottom area is a tableview to visualize results. 2.底部区域是用于可视化结果的tableview。

What I want 我想要的是

I want the controls area to take all necessary place (ie the place I gave into Interface Builder). 我希望控件区域占据所有必要的位置(即我给Interface Builder的地方)。 The TableView will shrink as needed. TableView将根据需要缩小。

Problem 问题

When loaded from a XIB the UIViewController.view frame has ALWAYS a size of 0x568. 从XIB加载时, UIViewController.view框架的大小始终为0x568。 I was expected 0x200 (if 200 was the size I configured in Interface Builder). 我被期待0x200(如果200是我在Interface Builder中配置的大小)。

What works : with AutoLayout 什么有效:使用AutoLayout

I created a XIB with all my element. 我用我的所有元素创建了一个XIB。 I did the outlet on the "root view" and I did outlet and all views containers. 我在“根视图”上做了插座,我做了插座和所有视图容器。

Then in my viewDidLoad method I added the containers and configured constraints. 然后在我的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];
}

With this, I can simple resize the topView view with Interface Builder and the TableView will be resized as needed (no compression resistance, I don't care if we can't see the table). 有了这个,我可以使用Interface Builder简单地调整topView视图的大小,并且TableView将根据需要调整大小(没有压缩阻力,如果我们看不到表,我不在乎)。

What DO NOT works : with AutoLayout and ChildControllers 什么AutoLayout :使用AutoLayoutChildControllers

Then for simplicity I choose to use ChildControllers (lot of outlet required to perform custom UIFont setup, too bad XCode can't handle them yet!). 然后为了简单起见,我选择使用ChildControllers (执行自定义UIFont设置所需的很多插座,太糟糕的XCode还无法处理它们!)。 I made the following modifications : 我做了以下修改:

  1. Created HomeTopViewController classes + XIB. 创建了HomeTopViewController类+ XIB。 Created HomeTableViewController classes + XIB. 创建了HomeTableViewController类+ XIB。
  2. Copied all views from the origin to the correct XIB. 将所有视图从原点复制到正确的XIB。 Add the UIViewController references + wired the outlets. 添加UIViewController引用+连接出口。
  3. The root container of HomeTopViewController is configured to 200px height. HomeTopViewController的根容器配置为200px高度。
  4. Wired my containers to the view outlet of my childs controllers. 将我的容器连接到我的孩子控制器的view插座。

Then I updated my setup code to the following : 然后我将我的设置代码更新为以下内容:

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

Problem 问题

No matter how I size the UIView container in HomeTopViewController the line CFShow(self.topViewController.view); 无论我如何在HomeTopViewController调整UIView容器的行CFShow(self.topViewController.view); will always give me frame = (0 0; 320 568) when I expected 200px. 当我预期200px时,总会给我frame = (0 0; 320 568)

I did configure the layout size to "Freeform" or "None". 我确实将布局大小配置为“自由格式”或“无”。

What I want to avoid as much as possible 我想尽可能避免的

I would hate having to create other outlets then View on HomeTopViewController and HomeTableViewController and storing the initial frame / height into a UIViewController property. 我不想创建其他插座然后在HomeTopViewControllerHomeTableViewControllerView并将初始帧/高度存储到UIViewController属性中。

Questions 问题

  1. At load time, are all controller.view.frame property sized on the screen ? 在加载时,屏幕上是否都调整了controller.view.frame属性的大小? (be it 3,5 or 4 inches) (是3,5或4英寸)
  2. How can I solve this without hardcoding the size somewhere // creating additionnal outlets ? 如何在不创建额外插座的情况下解决这个问题来解决这个问题?

This would be easier if you used a storyboard. 如果您使用故事板,这将更容易。 You can add container views to you main controller's view and size them how you like, and you automatically get view controllers embedded in these container views that are set to the correct size. 您可以向主控制器的视图添加容器视图,并根据需要调整它们的大小,并自动获取嵌入在这些容器视图中的视图控制器,这些视图控制器设置为正确的大小。 These container views (next to the regular UIView in the object list) are only available from a storyboard, not xibs. 这些容器视图(在对象列表中的常规UIView旁边)仅可从故事板中获得,而不是xib。 This will require almost no code, and you probably won't have to add any constraints manually. 这几乎不需要代码,您可能不必手动添加任何约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM