【问题标题】:How do you create a UITableView programmatically with Auto Layout?如何使用自动布局以编程方式创建 UITableView?
【发布时间】:2014-11-10 05:15:08
【问题描述】:

它似乎假设您使用的是框架而不是自动布局,因为您只能使用框架进行真正的初始化。我做错了吗?

【问题讨论】:

    标签: objective-c cocoa-touch uitableview uiview autolayout


    【解决方案1】:

    您可以通过编程方式使用自动布局。 您不需要使用框架初始化 UITableView。相反,您必须设置translatesAutoresizingMaskIntoConstraints = NO,将表视图添加到父视图,然后在代码中定义约束。 例如:

    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:tableView];
    NSDictionary *views =
          NSDictionaryOfVariableBindings(tableView);
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:
                                   @"H:|[tableView]|" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:
                                   @"V:|[tableView]|" options:0 metrics:nil views:views]];
    

    请参考https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/AdoptingAutoLayout/AdoptingAutoLayout.html 了解更多关于视觉格式的信息。您也可以选择不使用可视化格式并以编程方式描述约束。

    【讨论】:

    • 那个实例化器不包括样式设置器。
    • 我已经编辑了我的答案以修复初始化。我面前没有 XCode 来测试它。
    • 这就是我的想法,当 Auto Layout 是定位系统时,提供一个框架(无论是否为 0)似乎仍然很奇怪。
    • 我收到了一个 SIGABORT(链接已损坏)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多