简体   繁体   中英

iOS AutoLayout issue: Unable to simultaneously satisfy constraints

Steps:

  1. I have two tableview in a page, but show one tableView at a time;
  2. I created a view for tableview's header view(call it as headerView );
  3. I set the headerView for the current tableView(A) as tableHeaderView, and set nil for another tableView(B) as 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);

When I set headerView for tableView(N) the error is coming, detail please take a look at below logs. 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:

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:

"<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. So it can't add the 10 point margins (which the two constraints above call for). Setting them to 999 should let them be overridden gracefully.

See What is a 'UIView-Encapsulated-Layout-Width' constraint? for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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