iOS 自动布局问题 : Unable to simultaneously satisfy constraints

标签 ios objective-c iphone autolayout

脚步:

  • 我在一个页面中有两个 tableview,但一次显示一个 tableView;
  • 我为 tableview 的标题 View 创建了一个 View (将其称为
    标题查看 );
  • 我设置了标题查看 对于当前 tableView(A) 为
    tableHeaderView,并将另一个 tableView(B) 设置为 nil
    tableHeaderView(显示 A);
  • 我单击一个按钮以显示 tableView(B),所以我将 nil 设置为
    tableView(A) 的 tableHeaderView,设置 标题查看 作为
    tableView(B) 的 tableHeaderView;

  • 当我为 tableView(N) 设置 headerView 时,错误来了,详细请看下面的日志。
    控制台错误日志如下:
        2015-12-04 10:15:18.777 XXXApp[1223:360705] 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. 
    (
        "<NSLayoutConstraint:0x7f8f7bdd74b0 H:|-(10)-[UIView:0x7f8f7bdbbe00]   (Names: '|':MarketGoodsDetail:0x7f8f7bdb92d0 )>",
        "<NSLayoutConstraint:0x7f8f7bdd7500 H:[UIView:0x7f8f7bdbbe00]-(10)-|   (Names: '|':MarketGoodsDetail:0x7f8f7bdb92d0 )>",
        "<NSLayoutConstraint:0x7f8f7bdd8fa0 H:|-(0)-[MarketGoodsDetail:0x7f8f7bdb92d0]   (Names: '|':UIView:0x7f8f7bdd8b70 )>",
        "<NSLayoutConstraint:0x7f8f7bdd8ff0 H:[MarketGoodsDetail:0x7f8f7bdb92d0]-(0)-|   (Names: '|':UIView:0x7f8f7bdd8b70 )>",
        "<NSLayoutConstraint:0x7f8f7bddac40 H:[UIView:0x7f8f7bdd8b70]-(0)-|   (Names: '|':UIView:0x7f8f7bdda860 )>",
        "<NSLayoutConstraint:0x7f8f7bddace0 H:|-(0)-[UIView:0x7f8f7bdd8b70]   (Names: '|':UIView:0x7f8f7bdda860 )>",
        "<NSLayoutConstraint:0x7f8f7e0c6d90 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7f8f7bdda860(0)]>"
    )
    
    Will attempt to recover by breaking constraint 
    <NSLayoutConstraint:0x7f8f7bdd7500 H:[UIView:0x7f8f7bdbbe00]-(10)-|   (Names: '|':MarketGoodsDetail:0x7f8f7bdb92d0 )>
    
    Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
    The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
    

    杰森·贾勒特 的博客 http://staxmanade.com/2015/06/debugging-ios-autolayout-issues/我打印了 View 的自动布局信息,我认为它导致了以下问题:
    po [0x7f8f7bdb92d0 recursiveDescription]
    <MarketGoodsDetail: 0x7f8f7bdb92d0; frame = (0 0; 375 127); autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x7f8f7bdb9760>>
       | <UILabel: 0x7f8f7bdb9b80; frame = (10 8.5; 37.5 12); text = '(广贵所)'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdb9780>>
       |    | <_UILabelContentLayer: 0x7f8f7bfc4090> (layer)
       | <UILabel: 0x7f8f7bdba820; frame = (10 22.5; 121.5 36); text = '2892.00'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdb9f10>>
       | <UILabel: 0x7f8f7bdbb460; frame = (237.5 35; 52.5 20.5); text = '-13.00'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbb660>>
       | <UILabel: 0x7f8f7bdbb9c0; frame = (307 35; 58 20.5); text = '-0.45%'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbbbc0>>
       | <UIView: 0x7f8f7bdbbe00; frame = (10 63.5; 355 0.5); autoresize = RM+BM; layer = <CALayer: 0x7f8f7bdbbf70>>
       | <UILabel: 0x7f8f7bdbc0b0; frame = (10 76; 26 16); text = '今开'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbc2b0>>
       |    | <_UILabelContentLayer: 0x7f8f7e0c2a90> (layer)
       | <UILabel: 0x7f8f7bdbcd70; frame = (42 76; 51.5 16); text = '2905.00'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbcf70>>
       | <UILabel: 0x7f8f7bdbd180; frame = (10 102; 26 16); text = '昨收'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbd380>>
       |    | <_UILabelContentLayer: 0x7f8f7e0c1640> (layer)
       | <UILabel: 0x7f8f7bdbd590; frame = (42 102; 51.5 16); text = '2905.00'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbd790>>
       | <UILabel: 0x7f8f7bdbd9a0; frame = (281.5 76; 26 16); text = '最高'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbdba0>>
       |    | <_UILabelContentLayer: 0x7f8f7e0c1ea0> (layer)
       | <UILabel: 0x7f8f7bdbddb0; frame = (313.5 76; 51.5 16); text = '2909.00'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbdfb0>>
       | <UILabel: 0x7f8f7bdbe1c0; frame = (281.5 102; 26 16); text = '最低'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbe3c0>>
       |    | <_UILabelContentLayer: 0x7f8f7e0c4e60> (layer)
       | <UILabel: 0x7f8f7bdbe5d0; frame = (313.5 102; 51.5 16); text = '2889.00'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbe7d0>>
       | <UIView: 0x7f8f7bdbe9e0; frame = (146 76; 83.5 42); autoresize = RM+BM; layer = <CALayer: 0x7f8f7bdbeb50>>
       |    | <UILabel: 0x7f8f7bdbeb70; frame = (0 0; 26 16); text = '买进'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbed70>>
       |    |    | <_UILabelContentLayer: 0x7f8f7e0c4e90> (layer)
       |    | <UILabel: 0x7f8f7bdbef80; frame = (0 26; 26 16); text = '卖出'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdd5990>>
       |    |    | <_UILabelContentLayer: 0x7f8f7e0c4b70> (layer)
       |    | <UILabel: 0x7f8f7bdd5b80; frame = (32 0; 51.5 16); text = '2898.00'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdd5d80>>
       |    | <UILabel: 0x7f8f7bdd5f90; frame = (32 26; 51.5 16); text = '2892.00'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdd6190>>
    

    它是 super View 自动布局:
    po [[0x7f8f7bdb92d0 superview] recursiveDescription]
    <UIView: 0x7f8f7bdd8b70; frame = (0 0; 375 127); autoresize = RM+BM; layer = <CALayer: 0x7f8f7bdd8ce0>>
       | <MarketGoodsDetail: 0x7f8f7bdb92d0; frame = (0 0; 375 127); autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x7f8f7bdb9760>>
       |    | <UILabel: 0x7f8f7bdb9b80; frame = (10 8.5; 37.5 12); text = '(广贵所)'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdb9780>>
       |    |    | <_UILabelContentLayer: 0x7f8f7bfc4090> (layer)
       |    | <UILabel: 0x7f8f7bdba820; frame = (10 22.5; 121.5 36); text = '2892.00'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdb9f10>>
       |    | <UILabel: 0x7f8f7bdbb460; frame = (237.5 35; 52.5 20.5); text = '-13.00'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbb660>>
       |    | <UILabel: 0x7f8f7bdbb9c0; frame = (307 35; 58 20.5); text = '-0.45%'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbbbc0>>
       |    | <UIView: 0x7f8f7bdbbe00; frame = (10 63.5; 355 0.5); autoresize = RM+BM; layer = <CALayer: 0x7f8f7bdbbf70>>
       |    | <UILabel: 0x7f8f7bdbc0b0; frame = (10 76; 26 16); text = '今开'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbc2b0>>
       |    |    | <_UILabelContentLayer: 0x7f8f7e0c2a90> (layer)
       |    | <UILabel: 0x7f8f7bdbcd70; frame = (42 76; 51.5 16); text = '2905.00'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbcf70>>
       |    | <UILabel: 0x7f8f7bdbd180; frame = (10 102; 26 16); text = '昨收'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbd380>>
       |    |    | <_UILabelContentLayer: 0x7f8f7e0c1640> (layer)
       |    | <UILabel: 0x7f8f7bdbd590; frame = (42 102; 51.5 16); text = '2905.00'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbd790>>
       |    | <UILabel: 0x7f8f7bdbd9a0; frame = (281.5 76; 26 16); text = '最高'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbdba0>>
       |    |    | <_UILabelContentLayer: 0x7f8f7e0c1ea0> (layer)
       |    | <UILabel: 0x7f8f7bdbddb0; frame = (313.5 76; 51.5 16); text = '2909.00'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbdfb0>>
       |    | <UILabel: 0x7f8f7bdbe1c0; frame = (281.5 102; 26 16); text = '最低'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbe3c0>>
       |    |    | <_UILabelContentLayer: 0x7f8f7e0c4e60> (layer)
       |    | <UILabel: 0x7f8f7bdbe5d0; frame = (313.5 102; 51.5 16); text = '2889.00'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbe7d0>>
       |    | <UIView: 0x7f8f7bdbe9e0; frame = (146 76; 83.5 42); autoresize = RM+BM; layer = <CALayer: 0x7f8f7bdbeb50>>
       |    |    | <UILabel: 0x7f8f7bdbeb70; frame = (0 0; 26 16); text = '买进'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdbed70>>
       |    |    |    | <_UILabelContentLayer: 0x7f8f7e0c4e90> (layer)
       |    |    | <UILabel: 0x7f8f7bdbef80; frame = (0 26; 26 16); text = '卖出'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdd5990>>
       |    |    |    | <_UILabelContentLayer: 0x7f8f7e0c4b70> (layer)
       |    |    | <UILabel: 0x7f8f7bdd5b80; frame = (32 0; 51.5 16); text = '2898.00'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdd5d80>>
       |    |    | <UILabel: 0x7f8f7bdd5f90; frame = (32 26; 51.5 16); text = '2892.00'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f8f7bdd6190>>
    

    谁能告诉我是哪个约束导致了这个问题?感谢您的帮助!

    最佳答案

    我认为您想将这些约束的优先级设置为 999:

    "<NSLayoutConstraint:0x7f8f7bdd74b0 H:|-(10)-[UIView:0x7f8f7bdbbe00]   (Names: '|':MarketGoodsDetail:0x7f8f7bdb92d0 )>",
    "<NSLayoutConstraint:0x7f8f7bdd7500 H:[UIView:0x7f8f7bdbbe00]-(10)-|   (Names: '|':MarketGoodsDetail:0x7f8f7bdb92d0 )>"
    

    我认为这些与以下内容相冲突:
    "<NSLayoutConstraint:0x7f8f7e0c6d90 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7f8f7bdda860(0)]>"
    

    它指定您的容器 View 的宽度将为 0 点。所以它不能添加 10 点边距(上面的两个约束要求)。将它们设置为 999 应该可以优雅地覆盖它们。

    What is a 'UIView-Encapsulated-Layout-Width' constraint?了解更多信息。

    关于iOS 自动布局问题 : Unable to simultaneously satisfy constraints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34080090/

    相关文章:

    iphone - @synthesize window=_window 有什么作用?

    ios - 为什么 Keychain 比保存在文件中的加密数据更安全?

    iphone - CTCallCenter currentCalls 未更新 - 每次安装仅工作一次

    iOS表格 View 单元格图片下载

    ios - 在 Xcode 中设置自定义类适用于 iOS 9.1 模拟器,但不适用于 iOS 8.4

    ios - OpenGL ES iOS 属性问题

    ios - 无法在 IOS 中从 Facebook 获取笔记内容

    objective-c - 仅限类及其子类的属性

    iPhone,拍摄时如何检测图像的方向

    ios - 如何将渐变颜色设置为UIImageView边框