首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIViewController viewDidLoad

UIViewController viewDidLoad
EN

Stack Overflow用户
提问于 2013-01-03 17:29:54
回答 1查看 673关注 0票数 3

我有了一个新项目,我可以使用iOS 5+特性,所以我选择同时使用AutoLayout和UIViewController childControllers功能。

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

我想要什么

我想要控制区占据所有必要的位置(即我给接口生成器的位置)。TableView将根据需要收缩。

问题

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

如何工作:使用AutoLayout

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

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

代码语言:javascript
复制
-(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];
}

使用这种方法,我可以使用Interface简单地调整topView视图的大小,并根据需要调整TableView的大小(没有抗压缩能力,我不在乎我们是否看不到表)。

不起作用的东西:使用AutoLayout ChildControllers

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

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

然后,我更新了我的设置代码如下:

代码语言:javascript
复制
-(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中的大小,当我期望200 me时,行CFShow(self.topViewController.view);总是会给我frame = (0 0; 320 568)

我确实将布局大小配置为"Freeform“或"None”。

我想尽量避免的

我讨厌在HomeTopViewControllerHomeTableViewController上创建其他接口,然后将初始帧/高度存储到UIViewController属性中。

问题

  1. 在加载时,屏幕上是否有所有controller.view.frame属性的大小?(无论是3、5或4英寸)
  2. 我如何解决这个问题,而不硬编码某个地方的大小//创建额外的插座?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-03 20:45:39

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14144033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档