我有一个自定义的 UITableViewCell
,里面有 UITextView
、UICollectionView
和 UITableView
。它们之间有 10 像素的间距。在某些情况下,我只有 UITextView
,有时只有 Collection View 等。
当没有文本时 - 我必须将 UICollection View 向上移动 10 像素。如果我没有文本、tableView 和 collectionView,它的高度应该为零。
- (void)configureMiddleWrapperState
{
self.middleWrapperHasNote = self.noteTextView.text.length > 0;
self.noteTextViewTopConstraint.constant = self.middleWrapperHasNote ? 7.f : 0.f;
self.staffWrapperTopConstraint.constant = self.middleWrapperHasStaff ? 7.f : 0.f;
self.tagsWrapperTopConstraint.constant = self.middleWrapperHasTags ? 7.f : 0.f;
BOOL middleWrapperHasAtleastOne = self.middleWrapperHasNote || self.middleWrapperHasStaff ||
self.middleWrapperHasTags;
self.middleWrapperLastTenPixelSpaceConstraint.constant = middleWrapperHasAtleastOne ? 7.f : 0.f;
[self setNeedsUpdateConstraints];
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self setExcludedPaths];
[self configureMiddleWrapperState];
}
结果与预期不同: 而不是一行。 只有文本看起来像预期的那样。
只有当我多次上下滚动时,约束才会生效。
最佳答案
尝试添加
[self setNeedsLayout];
之后
[self setNeedsUpdateConstraints];
关于ios - UITableViewCell 约束未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41958884/