当使用自动布局约束以编程方式将viewController.view
添加到表视图单元时,有时无法正确布局。知道单元格具有固定的高度(50),并且子视图包含UIButton
和UITableView
,它们在开始时不包含单元格,直到用户执行某些操作为止。
以下是将子视图添加到单元格的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//Get sub item objects
BookItem * newItem = self.subItems[indexPath.row];
NSString * newItemKey = [NSString stringWithFormat:@"%i", newItem.iD];
NSString * currentCellItemKey = [NSString stringWithFormat:@"%i", cell.tag];
BookTreeItemViewController_iPad * bookTreeItemVC = self.subItemVCs[currentCellItemKey];
if (!bookTreeItemVC)
{
bookTreeItemVC = [self.storyboard instantiateViewControllerWithIdentifier:@"BookTreeItemViewController_iPad"];
//Clear cell
for (UIView * subView in cell.contentView.subviews)
[subView removeFromSuperview];
//Add sub item view to cell
[bookTreeItemVC.view setTranslatesAutoresizingMaskIntoConstraints:NO];
[cell.contentView addSubview:bookTreeItemVC.view];
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[item]|" options:nil metrics:nil views:@{@"item": bookTreeItemVC.view}]];
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[item]|" options:nil metrics:nil views:@{@"item": bookTreeItemVC.view}]];
}
bookTreeItemVC.bookItem = newItem;
//Replace the old book tree item view controller with the new one
[self.subItemVCs setValue:nil forKey:currentCellItemKey];
cell.tag = newItem.iD;
self.subItemVCs[newItemKey] = bookTreeItemVC;
return cell;
}
这是约束警告:
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)
(
"<NSLayoutConstraint:0x7c091240 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7c0895d0(49.5)]>",
"<NSLayoutConstraint:0x7a74f250 V:[UIButton:0x7a7482a0'Title']-(8)-[UITableView:0x7aad7a00]>",
"<NSLayoutConstraint:0x7a74f2b0 V:[_UILayoutGuide:0x7a74ee70]-(0)-[UIButton:0x7a7482a0'Title']>",
"<NSLayoutConstraint:0x7a74f310 V:[UITableView:0x7aad7a00]-(0)-[_UILayoutGuide:0x7a74ef60]>",
"<_UILayoutSupportConstraint:0x7a739510 V:[_UILayoutGuide:0x7a74ee70(87)]>",
"<_UILayoutSupportConstraint:0x7a74e2e0 V:|-(0)-[_UILayoutGuide:0x7a74ee70] (Names: '|':UIView:0x7a74ee00 )>",
"<_UILayoutSupportConstraint:0x7a747900 V:[_UILayoutGuide:0x7a74ef60(0)]>",
"<_UILayoutSupportConstraint:0x7a748130 _UILayoutGuide:0x7a74ef60.bottom == UIView:0x7a74ee00.bottom>",
"<NSLayoutConstraint:0x7a750c10 V:|-(0)-[UIView:0x7a74ee00] (Names: '|':UITableViewCellContentView:0x7c0895d0 )>",
"<NSLayoutConstraint:0x7a748270 V:[UIView:0x7a74ee00]-(0)-| (Names: '|':UITableViewCellContentView:0x7c0895d0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7a74f250 V:[UIButton:0x7a7482a0'Title']-(8)-[UITableView:0x7aad7a00]>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
顺便说一句,我从警告中注意到,顶部布局指南的高度为(87),应该为(0)
最佳答案
是,你说得对。
"<_UILayoutSupportConstraint:0x7a739510 V:[_UILayoutGuide:0x7a74ee70(87)]>",
不知何故,您的视图控制器的布局指南的最高支持值为87。由于固定单元格高度而导致崩溃,因此它消除了按钮与表格视图之间的距离限制。
也许您可以发布BookTreeItemViewController_iPad的布局代码,或者
关于ios - UITableViewCell中的NSLayoutConstraint中断,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26589280/