macOS 10.12自动布局问题

9
在10.12中有几个重大的变化,但我认为最大的变化是新的自动布局系统(或者是修改/重写的旧系统...谁知道呢)。我准备了一个简单的项目来演示这个问题。它涉及到通过按钮折叠分割视图项。这只会在10.12上发生。你需要做的就是编译项目并点击按钮。会出现一个错误:
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.
设置断点后,我们可以发现有问题的视图是:
(lldb) po $arg1
<NSSplitDividerView: 0x618000161980>

我是完全错误的,还是10.12存在明显问题?如果可能,请建议如何预防这种类型的错误。

项目可以从Github下载。

再次感谢您的帮助。

I. Nikolov


2
我可以通过调用NSAlert.layout()来触发此消息,以立即强制警报进行布局。由于在这种情况下没有建议可行且没有明显的方法可以避免错误,因此我提交了一个雷达(rdar:// 28700495)。 - rsfinn
4
我通过在NSSplitViewController中的splitView(_ splitView: NSSplitView, shouldHideDividerAt dividerIndex: Int) -> Bool方法中返回false来解决了这个问题。 - LShi
@LShi 你应该将你的修复方法发布为一个答案。这个方法对我有效。 - rob mayoff
你好,目前我在项目中没有使用这个“hack”,也没有出现任何错误。你的macOS版本是什么?我的是10.13。这种情况是在什么时候发生的?是在启动时还是在切换splitviewitem/sidebar时? - LShi
我的应用在macOS 10.13上运行时出现了一个新问题:有时在我的侧边栏展开(带动画)后,分割视图的分隔符会变成奇怪的虚线。上面介绍的技巧似乎解决了这个问题。 - LShi
显示剩余3条评论
1个回答

0

我遇到了相同的自动布局问题,但我在我的项目中找到了一个解决方法。在添加子视图及其约束后,我调用窗口进行“布局”。不幸的是,我必须在每个“add”调用之后这样做,否则系统会再次抛出丑陋的消息。这对我有用,我只在初始化期间调用它。

  // 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];

但是... 你应该同意这不是正确的布局方式... 肯定有些问题... 或者至少被误解了。无论如何 - 感谢您提供的信息。有几种类似的解决方法,但在我看来既不高效也不复杂。让我们希望未来事情会改变,或者我们将获得更多信息如何防止这种情况发生。 - Ivaylo Nikolov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接