我有一个代码在UITableView
的标头中设置了一个Autolayout。此代码在iOS9上运行正常,但在iOS8上它会引发UIViewAlertForUnsatisfiableConstraints
。
-(NSArray *)layoutConstraints
{
NSMutableArray * result = [NSMutableArray array];
NSDictionary * views = [self views];
NSDictionary * metrics = [self metrics];
[result addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[section]-20-|"
options:0
metrics:nil
views:views]];
[result addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[section]|"
options:0
metrics:nil
views:views]];
[result addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[subtitleLabel]-20-|"
options:0
metrics:nil
views:views]];
[result addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[titleLabel]-20-|"
options:0
metrics:metrics
views:views]];
[result addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[sectionContainer]|"
options:0
metrics:nil
views: views]];
[result addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[sectionContainer(section_heigth)]-[subtitleLabel]-[titleLabel]-20-|"
options:0
metrics:metrics
views:views]];
return result;
}
辅助方法是:
-(NSDictionary *)views
{
return @{ @"sectionContainer": self.sectionContainer,
@"section": self.section,
@"subtitleLabel": self.subtitleLabel,
@"titleLabel": self.titleLabel
};
}
-(NSDictionary *)metrics
{
return @{@"section_heigth" : @(HEIGHT_FOR_HEADER_IN_SECTION),
@"margin" :@20
};
}
提出的例外是:
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:0x1b954150 H:|-(20)-[UILabel:0x12834d40'Ability to Round'] (Names: '|':_UITableViewHeaderFooterContentView:0x239a5510 )>",
"<NSLayoutConstraint:0x1b954190 H:[UILabel:0x12834d40'Ability to Round']-(20)-| (Names: '|':_UITableViewHeaderFooterContentView:0x239a5510 )>",
"<NSLayoutConstraint:0x1b94f390 'UIView-Encapsulated-Layout-Width' H:[_UITableViewHeaderFooterContentView:0x239a5510(0)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x1b954190 H:[UILabel:0x12834d40'Ability to Round']-(20)-| (Names: '|':_UITableViewHeaderFooterContentView:0x239a5510 )>
查看自动布局的文档我没有发现任何可能影响此代码的从iOS8更改为iOS9的内容。我错过了什么吗?
答案 0 :(得分:0)
这里的问题不是约束本身。至少在你提供给我们的那部分代码中。
"<NSLayoutConstraint:0x1b94f390 'UIView-Encapsulated-Layout-Width' H:[_UITableViewHeaderFooterContentView:0x239a5510(0)]>"
系统会添加此类类型的内容,一旦设置了视图,就会将其删除。我的猜测是你在代码中的某处强制布局传递(例如 layoutIfNeeded )。 如果是这种情况,请删除它。请改用 setNeedsLayout 。该方法会将视图标记为 MUST_BE_LAYOUTED_IN_THE_NEXT_LAYOUT_PASS ,但在准备就绪之前不会强制显示。