【问题标题】:Constraining a UITableView to its superview's edges causes layout collision将 UITableView 约束到其父视图的边缘会导致布局冲突
【发布时间】:2017-08-24 16:11:29
【问题描述】:

我有一个简单的UITableView,我已将其限制在viewDidLoad 中的父视图边缘。

UITableView *tableView = [[UITableView alloc] init];
[tableView setBackgroundColor:[UIColor blackColor]];
tableView.delegate = self;
tableView.dataSource = self;
tableView.rowHeight = 44;
[tableView registerClass:Cell.self forCellReuseIdentifier:@"cell"];

[self.view addSubview:tableView];

[NSLayoutConstraint constraintWithItem:tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:0].active = YES;

[NSLayoutConstraint constraintWithItem:tableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0].active = YES;

[NSLayoutConstraint constraintWithItem:tableView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:0].active = YES;

[NSLayoutConstraint constraintWithItem:tableView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:0].active = YES;

[self.view layoutIfNeeded];

但是,当我构建和运行时,我在控制台中遇到了 2 个布局问题:

2017-08-24 12:08:21.676379-0400 objtest[62354:48633213] [LayoutConstraints] 无法同时满足约束。 以下列表中的至少一个约束可能是一个 你不想要。试试这个:(1)查看每个约束并尝试 找出你没想到的; (2) 找到添加的代码 不需要的约束或约束并修复它。 (注意:如果你是 看到你不理解的 NSAutoresizingMaskLayoutConstraints, 请参阅 UIView 属性的文档 translatesAutoresizingMaskIntoConstraints) ( "", “UITableView:0x7fcf0f017600(活动,名称: '|':UIView:0x7fcf0c604bf0)>", " (主动)>", "")

将尝试通过打破约束来恢复

在 UIViewAlertForUnsatisfiableConstraints 创建一个符号断点 在调试器中捕获它。中的方法 UIView 上的 UIConstraintBasedLayoutDebugging 类别列于 也可能有帮助。

2017-08-24 12:08:21.677146-0400 objtest[62354:48633213] [LayoutConstraints] 无法同时 满足约束。可能至少有一个约束条件 以下列表是您不想要的。试试这个:(1)看每个 约束并尝试找出您不期望的; (2) 找到 添加不需要的约束或约束并修复它的代码。 (注意:如果您看到 NSAutoresizingMaskLayoutConstraints 不明白,参考UIView属性的文档 translatesAutoresizingMaskIntoConstraints) ( "", "", " (主动)>", "")

将尝试通过打破约束来恢复

在 UIViewAlertForUnsatisfiableConstraints 创建一个符号断点 在调试器中捕获它。中的方法 UIView 上的 UIConstraintBasedLayoutDebugging 类别列于 也可能有帮助。

任何帮助都会很棒!谢谢!

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    看来你忘了:

    tableView.translatesAutoresizingMaskIntoConstraints = NO;
    

    【讨论】:

      猜你喜欢
      • 2018-02-02
      • 2014-11-04
      • 2020-09-07
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多