2

I'm trying to hide and show view #1 in the following picture based on whether the button is clicked using Autolayout. Anyone know how to do this?

I tried setting two NSLayoutConstraints for view #2, one where it is tied to the top of the superview of view #1 and view #2 and one where it is tied to the bottom of view #1, and then alter the priority of the NSLayoutConstraints to hide view #1, but that didn't seem to do anything.

Any advice would be appreciated. I'm mainly trying to do this in IB, but programatic solutions are welcome as well.

Pic for reference:

View test

Marek H
  • 5,173
  • 3
  • 31
  • 42
matthewjselby
  • 112
  • 2
  • 8
  • do you want view #1 to obscure the content of view #2 or can you just use an `NSSplitView`? – Brad Allred Nov 21 '13 at 16:39
  • I want view #1 to be hidden completely, with view #2 enlarging to take up that space. – matthewjselby Nov 21 '13 at 16:40
  • that didn't answer the question. can you just use an `NSSplitView`? for your purposes? – Brad Allred Nov 21 '13 at 16:43
  • Sorry for the confusion. I do not want view #1 to obscure view #2, and suppose that I could do this with NSSplitView. Still curious as to if there's a way I can do this with auto layout though. Looking into NSStackView (although it isn't very well documented). Thanks! – matthewjselby Nov 21 '13 at 17:11
  • if you want view #1 to obscure view #2 then you cannot use an `NSSplitView` also `NSSplitView` would use auto layout. 'NSStackView` is limited to 10.9 FYI. – Brad Allred Nov 21 '13 at 17:20
  • Anyway to use autolayout to change things like this you don't change the priority, but change the `constant` property of the constraint. you can even use this with the `animator` proxy of the constraint to animate the change. – Brad Allred Nov 21 '13 at 17:22

3 Answers3

2

NSStackView is appropriate here. It automates creating constraints that tie its subviews to each other in stack.

Hiding a view does not change layout. It's still there, just isn't drawing.

If you were doing it without NSStackView, what you would do is change the constraints. Keep an instance variable, _stackConstraints. In one configuration, the stack constraints would be

V:|-[0]-[view1(v1Height)]-0-[view2]-0-[view3(v3Height)]-0-|

and in the other configuration

V:|-[0]-[view2]-0-[view3(v3Height)]-0-| 

When you hit the button, do

[[self view] removeConstraints:_stackConstraints];
_stackConstraints = <make other set of constraints>
[[self view] addConstraints:_stackConstraints];
Ken
  • 12,933
  • 4
  • 29
  • 32
1

If you're willing to require 10.11+, you can just select "Detaches Hidden Views" on the NSStackView in Interface Builder (or set detachesHiddenViews = YES on it programmatically).

Then setting View #1 to hidden = YES will automatically re-layout the stack view, making View #2 take up more space (assuming the stack view height is fixed – if not, the stack view would just get less tall instead).


If you need to support 10.10 or earlier, then you can hide the view this way:

[stackView setVisibilityPriority:NSStackViewVisibilityPriorityNotVisible forView:view1];

And show it again via:

[stackView setVisibilityPriority:NSStackViewVisibilityPriorityMustHold forView:view1];
peterflynn
  • 4,667
  • 2
  • 27
  • 40
0

Answer here Hide autolayout UIView : How to get existing NSLayoutConstraint to update this one

With this category https://github.com/damienromito/UIView-UpdateAutoLayoutConstraints

//Hide View 
[myView1 hideByHeight:YES];
Community
  • 1
  • 1
Damien Romito
  • 9,801
  • 13
  • 66
  • 84