gpt4 book ai didi

ios - iOS8 上的自动布局错误,但 iOS9 上没有

转载 作者:可可西里 更新时间:2023-11-01 04:56:27 25 4
gpt4 key购买 nike

我有一段代码可以在 UITableView 的页眉中设置自动布局。此代码在 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 的任何更改可能会影响此代码。我错过了什么吗?

最佳答案

你的问题不是约束本身。至少在您提供给我们的那部分代码中。

    "<NSLayoutConstraint:0x1b94f390 'UIView-Encapsulated-Layout-Width' H:[_UITableViewHeaderFooterContentView:0x239a5510(0)]>"

这种类型的约束由系统添加,一旦 View 已经设置,它们就会被删除。我的猜测是您在代码中的某处强制进行了布局传递(例如使用 layoutIfNeeded)。如果是这种情况,请将其删除。请改用 setNeedsLayout。该方法会将 View 标记为 MUST_BE_LAYOUTED_IN_THE_NEXT_LAYOUT_PASS 但不会在准备就绪之前强制执行。

关于ios - iOS8 上的自动布局错误,但 iOS9 上没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34439219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com