【发布时间】:2014-10-27 14:01:20
【问题描述】:
当使用自动布局约束以编程方式将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)
【问题讨论】:
-
你能发布你的代码吗?以便于识别
-
我更新了帖子并添加了添加子视图及其约束的代码。
标签: ios objective-c uitableview autolayout nslayoutconstraint