2

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.

Apple's 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.

CCNStatusItem window after loading the view

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

CCNStatusItem window with extended DisclosureViewController

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: enter image description here

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

Sliding now works!

Community
  • 1
  • 1
Julian Vogels
  • 668
  • 1
  • 6
  • 21

2 Answers2

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.

Taylor
  • 3,183
  • 17
  • 18
  • You're right, the dashed constraint has a low priority (750). I was playing around with this, but with either priority it wouldn't budge. Generally, what you described would be the right way to go, but in my case this didn't work. @Ken Thomases answer seems to get to the bottom of the issue. – Julian Vogels Sep 08 '15 at 09:07
  • Ah, glad you found the root cause. This would fall into the 3rd case (the container has autoresizing mask constraints that prevent it from growing) – Taylor Sep 08 '15 at 20:38
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.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • I forked the CCNStatusItem repository and did the following changes: 1. Added `translatesAutoresizingMaskIntoConstraints` to the `userConentView` in `CCNStatusItemWindow.m`. 2. Pinned the userContentView to the edges with constraints in code Works great now! The only bummer is that now the window contents are shifted to the top, but that's another issue probably. – Julian Vogels Sep 08 '15 at 09:13
  • The offset to the top of the superview was constant, so I could just go ahead and adjust the nibs in IB. Problem solved, all good now. – Julian Vogels Sep 08 '15 at 10:11