Letting a custom view grow with a MultiLineLabel subview - macos

I have a simple custom view and within a Wrapping Label (Multi Line Label) as a subview. I used autolayout to set the constraints.
Now I want that the superview change its size to show all content of the wrapping label. How can I achieve that? The wrapping laben shows the content of the extra text field (Outlet: "textfeld").

You need to set the content hugging & compression resistance constraints on your label (among other constraints).
With the label selected, select the size inspector. At the bottom, you'll see the Constraints section:
These have default values of around 250 & 750. We need to boost these up to 1000. These constraints control the way that the label grows & shrinks relative to its content, but with such low priority, every other autolayout constraint you add will take priority. So we need to increase the priority of these constraints up to 1000 which marks them as required (or at a minimum, you need to increase the content compression resistance priorities).
Now you just implement other autolayout constraints around that which make sense. For example, if you always want it to have at least a 20 pixel border from each side, be 20 pixels from the top, and centered in your superview, you might set up constraints like this:
Of course, with these constraints, if the parent view isn't constrained to a max width, you won't have implicitly forced any word wrapping, so it might expand the parent view's width. We can add a max width constraint to our label too.

Related

NSTableView: make total width of all columns always be the width of the visible portion of the table

I have a NSTableView with 2 columns. I'd like to know if there is a way to make it so that the right edge of the right column is always on the right edge of the clip view (so that it never goes beyond the clip view and horizontal scrolling isn't enabled), yet maintaining the ability to resize the two columns by moving the separator between them. This is essentially the same behavior as a split view.
I know IB offers options to control the resizing of columns, but I can't seem to enforce the rule that the total width of the two columns must be the width of the clip view (the visible portion of the table).

Using autolayout, how can I center a subview in an NSSplitView without forcing the width NSSplitView to be static?

I have an NSSplitView with content in both NSViews. The left NSView has 2 constraints – Equal Widths and Equal Heights. The right NSView has something simple, say an NSTextField, which is centered via constraints Center X Alignment and Center Y Alighment. This is what I hoped it would look like as I resize the window and/or the NSSplitView divider:
This is what's happening:
I've tried a great deal of configuration changes, I've tried using an NSSplitViewController vs just dropping an NSSplitView into an NSViewController to adjust more parameters programmatically, but I'm not having any luck. Whenever resizing the window, the left view always takes over the excess space. The same happens with the divider (it can be resized, but letting go of the mouse button causes it to snap right back). It seems there's something fundamental that I'm missing here.
The text field's content hugging priority is probably higher than the split view item's holding priority. Fix that and the view should probably work the way you expect.
Also, if, when you resize the view, the left view is resizing with the window while the right view stays the same size, then that suggests that the left view's holding priority may be higher than the right's. You should make the side that you want to stay the same size have the higher holding priority.
That said, I'm not sure what you mean about the constraints you've set on the subviews. "The left NSView has 2 constraints – Equal Widths and Equal Heights." What do you mean here? Its width is equal to what? Its height is equal to what? Do you mean it has an aspect ratio constraint? Frankly, I can't think of what constraints of those kinds would make sense for a view within a split view.

NSView preventing window/content view from resizing horizontally

I have an NSView inside my main view that is preventing my window/main view from resizing correctly. Even when I go fullscreen, My main view can't fill the screen (there's some black space at the right). I know that the cause is that particular view (or something inside) as when I delete it my app behaves normally. What would cause an NSView to "control" growing of its window?
Here are the constraints on that view:
UPDATE: I've found an NSTextField inside which had hugging proiorty set to 750. I've taken it down to 250, now it DOES grow, but I can't shrink it down from "some" size. I'm calling it "some" size because it has nothing special: it's a bit less than my native fullscreen width, and it's different than my IB width.
In general, constraints with priorities higher than NSLayoutPriorityWindowSizeStayPut (500) can force the size of a window. That includes the implicit constraints generated by intrinsic content size, if a view has such, which have the priorities set for content hugging and compression resistance.
So, if you have a text field whose horizontal content hugging priority is, say, 750 and there's a chain of constraints that connect its leading and trailing edges to the window's content view's edges (or, similarly, relate the text field's width to the content view's width), then the window won't be able to grow large enough to "stretch" that text field.
Likewise, if the text field's horizontal compression resistance is high, the window won't be able to shrink to the point where the text field would have to be compressed.

Auto Layout - Vertical spacing

I'm Ctrl dragging from one UILabel to another to set a vertical spacing and I get the following dialog:
What does this dialog mean?
How do I set a vertical spacing between the 2 UILabels using this dialog?
Apparently, you already have created a vertical spacing constraint. That's what the dot (•) next to Vertical Spacing means.
If you were starting from scratch, you would select Vertical Spacing. That would create a constraint which maintains the current vertical spacing between the labels as they are laid out on the canvas. You could then select that constraint and modify it, if you want. You can find the constraint in the document outline or you can look at the Size inspector for one of the affected views.
In general, that window lets you create one or more constraints based on the current layout of the designated views. Clicking on one of the items creates that one constraint. Shift-clicking on an item selects it and leaves the panel up so you can select more. When you've selected all that you want, click outside of the panel to create all the specified constraints.
Choosing "Left" creates a constraint relating the left (or leading) side of the views, with a constant for their current distance. Similar for "Center X" and "Right", except relating different attributes of the views.
Equal Widths creates a constraint relating the widths of the views, with a constant for the current difference in their widths. Similar for Equal Heights. Aspect Ratio creates a constraint relating the width of one to the height of the other (it makes most sense when you create a constraint from a view to itself).

How to have a view resize using Xcode's auto layout

I am using Xcode's auto layout feature for the first time in a project where I have several NSPopUpButtons.
Now what I want to achieve is to have two popUpButtons in a row together with their labels and when the window is resized I want both popUpButtons to adjust their width while keeping the horizontal spacing between each other.
However no matter how I apply the constraints I just don't get the popUpButtons to change their size with the window. They will always break their horizontal spacing constraints and just increase/decrease the spacing to the labels. I hope it gets a bit clearer what I have done from this screenshot:
I have set the spacings between the labels and the popUpButtons to fixed values with 1000 priority and have set the width constraints fo the popUpButtons to be greater or equal to the initial size.
How must I set my constraints to have the popUpButtons resize?
While writing this question I realized what the trick is:
In the size inspector of the NSPopUpButton I had to reduce the Content Hugging Priority.
Obviously this controls how closely the view wants to 'hug' its content. So when the hugging priority is higher than the resize priority the view will not want to increase its size because that would mean to have more empty space between its bounds and its content.
Then in my special case, I could also pin both NSPopUpButtons to have the same width and voilà: the popUpButtons will perfectly resize while keeping the spacing constant.

Resources