,首先,这与动态细胞的高度无关。所以别搞混了。
我有一个场景,我创造了三张卡片
下面是上面卡片的屏幕:



以上屏幕的视图层次结构:
所以我在这里展示了最后一张卡的约束条件,所有上面的卡片都是完美的!没有任何限制、模糊或警告。
让我们看一看代码:
Card-3 首先将添加到 ScrollView**:**中,chartBox是Card-2,bottomBox是Card-3。
[self.scrollView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:[chartBox(200)]-(10)-[bottomBox]"
options:0
metrics:nil
views:@{@"bottomBox" : self.bottomBox, @"chartBox" : self.chartBox}]];
// Below constraint pin to increase content-size
[self.scrollView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:[bottomBox]-(10)-|"
options:0
metrics:nil
views:@{@"bottomBox" : self.bottomBox}]];
[self.scrollView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:[bottomBox]-(10)-|"
options:0
metrics:nil
views:@{@"bottomBox" : self.bottomBox}]];CustomView 第二次将添加到 Card-3**:** Here bluePrintView is CustomView
_bluePrintView = [[BluePrintView alloc] init];
self.bluePrintView.translatesAutoresizingMaskIntoConstraints = NO;
[self.bottomBox addSubview:self.bluePrintView];
[self.bottomBox addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[bluePrintView]|" options:0 metrics:nil views:@{@"bluePrintView":self.bluePrintView}]];
[self.bottomBox addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[bluePrintView]|" options:0 metrics:nil views:@{@"bluePrintView":self.bluePrintView}]];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width - 20;
NSDictionary *metrices = @{@"width" : [NSNumber numberWithFloat:screenWidth]};
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(10)-[bottomBox(width)]" options:0 metrics:metrices views:@{ @"bottomBox": self.bottomBox}]];第三,在TableView CustomView**:**中添加
self.tableView.tableFooterView = [[UIView alloc] init];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tableView]|" options:0 metrics:nil views:@{@"tableView":self.tableView}]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[tableView]|" options:0 metrics:nil views:@{@"tableView":self.tableView}]];
// Height constraint
CGFloat viewHeight = self.bounds.size.height;
self.tableHeightConstraint = [NSLayoutConstraint constraintWithItem:self.tableView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:viewHeight];
[self addConstraint:self.tableHeightConstraint];
[self.tableView addObserver:self forKeyPath:@"contentSize" options:0 context:NULL];在观察者方法中,我将更新tableHeightConstraint约束。
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
self.tableHeightConstraint.constant = self.tableView.contentSize.height;
[self.tableView setNeedsLayout];
[self.tableView layoutIfNeeded];
}在尝试了所有的可能性之后,就到了这里。关于约束的警告。
(
"<NSLayoutConstraint:0x7fbeb1e7e8e0 V:|-(0)-[UITableView:0x7fbeb2843000] (Names: '|':BluePrintView:0x7fbeb1e7ac30 )>",
"<NSLayoutConstraint:0x7fbeb1e7e700 V:[UITableView:0x7fbeb2843000]-(0)-| (Names: '|':BluePrintView:0x7fbeb1e7ac30 )>",
"<NSLayoutConstraint:0x7fbeb1e7e9b0 V:[UITableView:0x7fbeb2843000(480)]>",
"<NSLayoutConstraint:0x7fbeb1e81a20 '_UITemporaryLayoutHeight' V:[BluePrintView:0x7fbeb1e7ac30(0)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fbeb1e7e700 V:[UITableView:0x7fbeb2843000]-(0)-| (Names: '|':BluePrintView:0x7fbeb1e7ac30 )>我通过从|中删除[tableView]行(通过将底部约束移除到superView)来解决这些警告,但是tableView没有得到所需的高度。
知道如何在没有任何约束警告的情况下获得完整的tableView高度。
所有视图都是由代码创建的,没有XIB没有故事板布局。iOS 9.2
发布于 2016-02-12 14:05:05
在约束设置中,我没有看到任何错误。
您可能会看到这个UITemporaryLayoutHeight约束,因为您在视图生命周期中过早地添加了contentSize观察者([self.tableView addObserver:self forKeyPath:@"contentSize" options:0 context:NULL] ),这会触发[self.tableView layoutIfNeeded]。
尝试在viewDidLoad中添加观察者,在视图控制器的dealloc中删除它;或者在viewWillAppear中删除它,例如在viewWillDisappear中删除它。
https://stackoverflow.com/questions/35363292
复制相似问题