Autolayout: NSStackView won't resize parent view when height dynamically changed

≯℡__Kan透↙ 提交于 2020-01-13 21:37:47

问题


Similar question: How to resize a parent view based on the size of subviews layouted with Autolayout

I got an NSStackView which loads DisclosureViewControllers (NSViewController subclasses) just like in the Apple Example InfoBarStackView.

Those can expand and retract views of arbitrary height. I would like the parent view containing the NSStackView to resize its height according to the contents of the NSStackView. In the Apple example that works.

However, unfortunately, Apple is resizing a NSWindow and I am using a CCNStatusItem (CCNStatusItem Github), a view that attaches to the status item in the Apple menu bar. That window resizes to the contents of the NSStackView when it loads, but doesn't resize when the contents change.

When I expand a section, the content overflows and gets clipped on the bottom edge of the view.

I went through all the constraints and tried to reproduce it exactly, but couldn't get it to work. In the Apple example, they are adding everything programmatically, whereas I added a NSStackView in Interface Builder.

Here are the constraints of the NSStackView:

My question would be: What constraints do I have in the Interface Builder (with what priorities), so that the parent view (the window) resizes with the contents of the stack view dynamically? Do I have to call some method of the view to make it work?

Please let me know if I missed to provide some necessary information.

UPDATE

The accepted answer was the right way to do it. Here's the result. My forked version of CCNStatusItem can be found at https://github.com/julianvogels/CCNStatusItem.git


回答1:


The problem is that CCNStatusItem is not auto-layout-compatible. It sets the content view of its window to an instance of one of its own view classes (CCNStatusItemWindowBackgroundView). Your view is a subview of that view.

When it creates and adds its view, it does not turn off translatesAutoresizingMaskIntoConstraints or use constraints to position it relative to its superview. That basically means it can't be forced to a different size by constraints.

Likewise, when it adds your view to the background view, it does not turn off translatesAutoresizingMaskIntoConstraints on your view nor does it set up constraints to relate your view to the background view.

Since you've got the source, you can make those changes.




回答2:


When using auto layout constraints, you don't need to call a view method to cause this effect, but just make sure that:

  • the views inside the stack view have constraints that will cause them to grow
  • the container has constraints to the stack view that indicate the container should grow with the stack view
  • the container doesn't have any constraints that would prevent it from growing

The screenshots seem to indicate it's not the first -- the views are growing as expected. So it could be either the second or third. The screenshot from interface builder seems to show that the bottom StackView<>Container constraint is optional (it's dashed). Depending on what the priority actually is, that could be your problem. Based on the design you described, there should be no reason for that constraint to be non-required.



来源:https://stackoverflow.com/questions/32245528/autolayout-nsstackview-wont-resize-parent-view-when-height-dynamically-changed

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