I am coding an MacOS app with swift. The main layout in the window is like follows:
The NSStackView has a sideBar (NSTableView) and a NSView. What I want to achieve is listed here:
- The width of the sideBar is fixed when changing the window size.
- When the width of the window is smaller than
WIDTH_THRESHOLD, the sideBar is hidden.
The problems are
- For Target 1, the constraints of the sideBar and the NSView is like follows, but it doesn't work and the width of the sideBar still changes.
- For Target 2, the project has a
NSWindowControllerand aNSViewController. I can listen the window size inNSWindowController, but I don't know how to transport this msg to theNSViewControllerto hidden the sideBar.
Notice that NSStackView has the properties like Hugging Priority, Clipping Resistance Priority and Visibility Priorities in size inspector, can I achieve the targets through setting these properties (in the following figure)?



Have you constrained the width of the sidebar? If you haven't, what may be happening is that the
NSViewis autoresizing and pushing the sidebar around. Changing the red arrows doesn't create actual constraints, just changes the resizing rules.Listen to the window size in your
NSWindowController, just like you said. As long as you have a reference to your side bar available in that window class (e.g. you can access the view controller that contains it), you can setsideBar.isHidden = truewhen the window gets too small, andfalsewhen the window is large enough again.