I have a NSWindow, with some views, hooked together with auto layout constraints.
Window looks something like this...
Label - TextView - Label - TextView - Label - DatePicker
Label - TextView - Label - PopUp - Label - PopUp
The data for the popups is obtained via bindings to core data, so the values are not known until runtime. I want those controls to resize to accommodate the values from the database.
On the surface, everything seems to be fine. However, if I end up with a really long string in a popup control, the size of that control causes the size of the window to expand.
I want the sizes to be dynamic, and change according to the values of the data, but I want the constraints to be limited to the size of the window.
However, I want the user to be able to resize the window, if they want to make it wider. I only want the window to be resized by the user (the window size will be saved and restored automatically).
I sort of get that if I set the hugging value on the main view of the window, but that seems to prevent the user from being able to resize the window as well. If I reduce the priority of the controls' constraints, I don't get the size and positioning as I want it.
So, basically, I want the controls to change as the window size changes, but I don't want the size of the window to change unless to user changes it. How can I get that to happen?
Thanks!
You will need to change the content hugging and compression resistance priorities of your views to be less than NSLayoutPriorityWindowSizeStayPut
(500).
If the content hugging priorities are higher than that, then the window won't be able to be resized to be so large that those views get stretched. If the compression resistance priorities are higher than that, then the window won't be able to be resized to be so small that those views get compressed.
You say "[i]f [you] reduce the priority of the controls' constraints, [you] don't get the size and positioning as [you] want it". Well, what breaks? You'll need to find a way to achieve what you want even with the content hugging and compression resistance priorities reduced.
You may want to put minimum width constraints on your views, too, to prevent the window from compressing them to nothing. Collectively, such constraints will establish the minimum size for your window (which you probably do want).
Also, you probably want the priorities to be different among the views or you'll get ambiguity and somewhat arbitrary results. That is, if the user shrinks the window so not everything can fit, and if two or more views have the same compression resistance priority, then it will be arbitrary which is compressed. It can even flip back and forth disconcertingly as further changes are made. So, you should effectively nominate which should compress first, then second, etc., by ordering their compression resistance priorities. Similarly for content hugging.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments