简体   繁体   English

iOS AutoLayout问题:无法同时满足约束

[英]iOS AutoLayout issue: Unable to simultaneously satisfy constraints

Steps: 脚步:

  1. I have two tableview in a page, but show one tableView at a time; 我在一个页面中有两个tableview,但是一次只显示一个tableView。
  2. I created a view for tableview's header view(call it as headerView ); 我为tableview的标题视图创建了一个视图(称为headerView );
  3. I set the headerView for the current tableView(A) as tableHeaderView, and set nil for another tableView(B) as tableHeaderView(show A); 我将当前tableView(A)的headerView设置为tableHeaderView,将另一个tableView(B)的nil设置为tableHeaderView(show A);
  4. I click a button to show tableView(B), so I set nil as tableHeaderView for tableView(A), set headerView as tableHeaderView for tableView(B); 我单击一个按钮以显示tableView(B),因此将nil设置为tableView(A)的tableHeaderView,将headerView设置为tableView(B)的tableHeaderView。

When I set headerView for tableView(N) the error is coming, detail please take a look at below logs. 当我为tableView(N)设置headerView时,错误即将来临,详细信息请查看以下日志。 The console error log as below: 控制台错误日志如下:

    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.

As Jason Jarrett 's blog http://staxmanade.com/2015/06/debugging-ios-autolayout-issues/ I printed the auto layout info of the view which I think it caused the problem as below: Jason Jarrett的博客http://staxmanade.com/2015/06/debugging-ios-autolayout-issues/时,我打印了该视图的自动布局信息,我认为它引起了以下问题:

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

And it's super view auto layout: 它是超级视图自动布局:

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

Who can tell me which constraint caused this problem? 谁能告诉我哪个约束导致了此问题? Thanks for kindly help!! 感谢您的帮助!

I think you want to set the priority of these constraints to 999: 我认为您要将这些约束的优先级设置为999:

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

I believe those are conflicting with: 我认为这些与以下内容冲突:

"<NSLayoutConstraint:0x7f8f7e0c6d90 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7f8f7bdda860(0)]>"

Which specifies your container view's width is going to be 0 points. 它指定您的容器视图的宽度将为0点。 So it can't add the 10 point margins (which the two constraints above call for). 因此,它不能添加10个点的边距(以上两个约束要求该点)。 Setting them to 999 should let them be overridden gracefully. 将它们设置为999应该可以优雅地覆盖它们。

See What is a 'UIView-Encapsulated-Layout-Width' constraint? 请参阅什么是“ UIView-Encapsulated-Layout-Width”约束? for more info. 有关更多信息。

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

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