ios - 将 UITableView 约束到其父 View 的边缘会导致布局冲突

标签 ios objective-c uitableview

我有一个简单的 UITableView,我在 viewDidLoad 中限制了它的 super View 的边缘。

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] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "", "UITableView:0x7fcf0f017600 (active, names: '|':UIView:0x7fcf0c604bf0 )>", " (active)>", "" )

Will attempt to recover by breaking constraint

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.

2017-08-24 12:08:21.677146-0400 objtest[62354:48633213] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "", "", " (active)>", "" )

Will attempt to recover by breaking constraint

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.

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

最佳答案

看起来你忘记了:

tableView.translatesAutoresizingMaskIntoConstraints = NO;

关于ios - 将 UITableView 约束到其父 View 的边缘会导致布局冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45866460/

相关文章:

ios - 为什么表格 View 不显示 iphone 中的任何数据

iphone - 找不到协议(protocol)声明自定义协议(protocol)委托(delegate) iphone

ios - 选择器未被识别

iphone - 更改 UITableViewCell 中 textLabel 和 detailTextLabel 之间的间距?

ios - 如何从纯 C 函数获取返回值到 swift?

iphone - Objective-C 中小部件 API 的最佳实践

ios - Core Data存储40万条数据用于地铁寻路很不正常?

objective-c - 如何解决 fileAttributesAtPath 警告的问题?

ios - 呈现 View Controller 时的 UIViewControllerHierarchyInconsistency

ios - 为什么 tableview 没有显示正确的图像?