【问题标题】:Adding AutoLayout Constraints To A UITableView When Adding To Parent View添加到父视图时向 UITableView 添加 AutoLayout 约束
【发布时间】:2017-10-31 15:45:28
【问题描述】:

我正在尝试使用 Autolayout 在我的 UITableView 中创建以下设置,然后将其添加为其包装 UIView 的子视图。

到目前为止,我正在使用以下内容,但一点运气都没有。这是据我所知,它正在引发错误。

//Trailing
    NSLayoutConstraint *trailing =[NSLayoutConstraint
                                   constraintWithItem:self.tableViewVideoList
                                   attribute:NSLayoutAttributeTrailing
                                   relatedBy:NSLayoutRelationEqual
                                   toItem:self.viewContentWrapper
                                   attribute:NSLayoutAttributeTrailing
                                   multiplier:1.0f
                                   constant:0.f];

    //Leading

    NSLayoutConstraint *leading = [NSLayoutConstraint
                                   constraintWithItem:self.tableViewVideoList
                                   attribute:NSLayoutAttributeLeading
                                   relatedBy:NSLayoutRelationEqual
                                   toItem:self.viewContentWrapper
                                   attribute:NSLayoutAttributeLeading
                                   multiplier:1.0f
                                   constant:0.f];

    //Bottom
    NSLayoutConstraint *bottom =[NSLayoutConstraint
                                 constraintWithItem:self.tableViewVideoList
                                 attribute:NSLayoutAttributeBottom
                                 relatedBy:NSLayoutRelationEqual
                                 toItem:self.viewContentWrapper
                                 attribute:NSLayoutAttributeBottom
                                 multiplier:1.0f
                                 constant:0.f];

    [self.viewContentWrapper addConstraint:trailing];
    [self.viewContentWrapper addConstraint:bottom];
    [self.viewContentWrapper addConstraint:leading];


    [self.viewContentWrapper addSubview:self.tableViewVideoList];

任何帮助都会非常感谢,我通常会在 Storybaord 中完成所有自动布局工作。

【问题讨论】:

  • 您面临哪些错误?
  • 在添加固定约束之前需要将表视图添加为子视图
  • 我认为您还需要通过添加顶部约束或显式设置高度来设置 tableview 的高度

标签: ios objective-c autolayout


【解决方案1】:

你可能忘了这样做:

self.tableViewVideoList.translatesAutoresizingMaskIntoConstraints = NO;

并且你应该在设置约束之前将 `tableViewVideoList' 添加到它的超级视图中。

而且您缺少顶部布局约束或高度约束(正如 3stud1ant3 指出的那样)

【讨论】:

    猜你喜欢
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多