当前位置:  开发笔记 > IOS > 正文

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

如何解决《iOS8上的自动布局错误,但iOS9上没有》经验,为你挑选了0个好方法。

我有一个代码,在一个标题中设置一个Autolayout 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) 
(
    "",
    "",
    ""
)

Will attempt to recover by breaking constraint 

查看自动布局的文档我没有发现任何可能影响此代码的从iOS8到iOS9的更改.我错过了什么吗?

推荐阅读
wurtjq
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有