macOS 10.12 auto-layout issues

允我心安 提交于 2019-12-12 09:36:12

问题


There are several huge changes in 10.12, but in my opinion the biggest is the new auto-layout system (or modified/rewritten old one ... who knows). I have prepared a simple project to demonstrate the issue. It's related with collapsing a split view item via a button. It happens only on 10.12. All you need to do is to compile the project and click the button. An error will be presented:

2016-10-04 15:10:28.284296 test-12[64932:7425277] [Layout] Detected missing constraints for . It cannot be placed because there are not enough constraints to fully define the size and origin. Add the missing constraints, or set translatesAutoresizingMaskIntoConstraints=YES and constraints will be generated for you. If this view is laid out manually on macOS 10.12 and later, you may choose to not call [super layout] from your override. Set a breakpoint on DETECTED_MISSING_CONSTRAINTS to debug. This error will only be logged once.

After setting a breakpoint we can find out that the problematic view is:

(lldb) po $arg1
<NSSplitDividerView: 0x618000161980>

Am I completely wrong or there is a definite problem with 10.12? And please advice how to prevent such type of errors if possible.

The project can be downloaded from github

Once again thank you for the help.

I. Nikolov


回答1:


I have the same auto-layout issue and I found a workaround for my project. After adding the subview with its constrains I call the window to "layout". Unfortunately I do that after each "add" call, otherwise the system will throw the ugly message again. It works for me and I am just calling it during initialistion.

  // Left Split View
  [self.scrollviewMain setTranslatesAutoresizingMaskIntoConstraints:NO];
  [self.viewMainLeft addSubview:self.scrollviewMain];
  [self.viewMainLeft addConstraint:[NSLayoutConstraint constraintWithItem:self.viewMainLeft attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.scrollviewMain attribute:NSLayoutAttributeBottom multiplier:1.0 constant:l_floatConstant]];
  [self.viewMainLeft addConstraint:[NSLayoutConstraint constraintWithItem:self.viewMainLeft attribute:NSLayoutAttributeTop    relatedBy:NSLayoutRelationEqual toItem:self.scrollviewMain attribute:NSLayoutAttributeTop    multiplier:1.0 constant:-l_floatConstant]];
  [self.viewMainLeft addConstraint:[NSLayoutConstraint constraintWithItem:self.viewMainLeft attribute:NSLayoutAttributeLeft   relatedBy:NSLayoutRelationEqual toItem:self.scrollviewMain attribute:NSLayoutAttributeLeft   multiplier:1.0 constant:-l_floatConstant]];
  [self.viewMainLeft addConstraint:[NSLayoutConstraint constraintWithItem:self.viewMainLeft attribute:NSLayoutAttributeRight  relatedBy:NSLayoutRelationEqual toItem:self.scrollviewMain attribute:NSLayoutAttributeRight  multiplier:1.0 constant:l_floatConstant]];
  [self.windowMain layoutIfNeeded];
  // Right Split View
  [self.scrollviewDetails setTranslatesAutoresizingMaskIntoConstraints:NO];
  [self.viewMainRight addSubview:self.scrollviewDetails];
  [self.viewMainRight addConstraint:[NSLayoutConstraint constraintWithItem:self.viewMainRight attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.scrollviewDetails attribute:NSLayoutAttributeBottom multiplier:1.0 constant:l_floatConstant]];
  [self.viewMainRight addConstraint:[NSLayoutConstraint constraintWithItem:self.viewMainRight attribute:NSLayoutAttributeTop    relatedBy:NSLayoutRelationEqual toItem:self.scrollviewDetails attribute:NSLayoutAttributeTop    multiplier:1.0 constant:-l_floatConstant-1]];
  [self.viewMainRight addConstraint:[NSLayoutConstraint constraintWithItem:self.viewMainRight attribute:NSLayoutAttributeLeft   relatedBy:NSLayoutRelationEqual toItem:self.scrollviewDetails attribute:NSLayoutAttributeLeft   multiplier:1.0 constant:-l_floatConstant]];
  [self.viewMainRight addConstraint:[NSLayoutConstraint constraintWithItem:self.viewMainRight attribute:NSLayoutAttributeRight  relatedBy:NSLayoutRelationEqual toItem:self.scrollviewDetails attribute:NSLayoutAttributeRight  multiplier:1.0 constant:l_floatConstant]];
  [self.windowMain layoutIfNeeded];


来源:https://stackoverflow.com/questions/39852109/macos-10-12-auto-layout-issues

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!