简体   繁体   English

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

[英]Auto layout error on iOS8 but not on iOS9

I have a code that set an Autolayout in a Header of an UITableView . 我有一个代码在UITableView的标题中设置Autolayout。 This code is working fine on iOS9 but on iOS8 it raises an UIViewAlertForUnsatisfiableConstraints . 这段代码在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;
}

The auxiliary methods are: 辅助方法是:

-(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
             };
}

The exception raised is: 提出的例外是:

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 )>

Looking into the documentation for Auto-layout I haven't found anything that changed from iOS8 to iOS9 that might impact this code. 查看自动布局的文档我没有发现任何可能影响此代码的从iOS8到iOS9的更改。 Am I missing something? 我错过了什么吗?

Your problem here are not the constraints itself. 这里的问题不是约束本身。 At least in that portion of code you provided us. 至少在你提供给我们的那部分代码中。

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

This type of constriants are added by the system, and they are removed once the view is already set up. 系统会添加此类类型的constriants,并且一旦已经设置了视图,它们就会被删除。 My guess is that you are forcing a layout pass somewhere in your code (with layoutIfNeeded for example). 我的猜测是你在代码中的某处强制布局传递(例如,使用layoutIfNeeded )。 If that's the case, remove that. 如果是这种情况,请删除它。 Use setNeedsLayout instead. 请改用setNeedsLayout That method will mark the view as MUST_BE_LAYOUTED_IN_THE_NEXT_LAYOUT_PASS but will not force it before it's ready. 该方法将视图标记为MUST_BE_LAYOUTED_IN_THE_NEXT_LAYOUT_PASS,但在准备好之前不会强制它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM