Question

I have a custom view controller class. I want to instantiate multiple custom view controllers and add their views to my NSStackView. I add a new view to the stack view by clicking a button. The button calls this method:

[stackView insertView:myCustomViewController.view atIndex:0 inGravity:NSStackViewGravityBottom];

However, when a new view is added, it's added stackView.spacing below where the previous view was, but that previous view is no longer visible, however it is still listed as a view in my bottom gravity as evidenced when I call

NSLog(@"%lu",(unsigned long)[stackView viewsInGravity:NSStackViewGravityBottom].count);

I don't have this problem if I try adding NSButtons, so it's something to do with my custom view but I can't figure out what.

Please help. Thanks!

Was it helpful?

Solution

You should check that your custom view has constraints (or an intrinsic content size) that completely define the height of the view. If they don't, these views are collapsing down to 0 heights (this same thing has happened to me before).

You can check if this is happening by:

[stackView insertView:myCustomViewController.view atIndex:0 inGravity:NSStackViewGravityBottom];
[stackView layoutSubtreeIfNeeded];
NSLog(@"%f - %d", NSHeight(myCustomViewController.view.frame), [myCustomViewController.view hasAmbiguousLayout]);


With StackView, you'll see that the later ones print "0 - 1". The heights are 0, and they're ambiguous.

Vertical NSStackView's will only position views vertically, but won't give specific heights. (Same for horizontal stack views and widths).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top