Set the width of a view to 50% of its container in the nib - macos

I am creating a view with four table views in it. I want each table view to take 50% of the height and width of the containing view and to auto-resize when the window is resized.
I have been unable to find a way to do this thus far.

You should use autolayout in storyboard. Resizing and stuff like that becomes very easy that way. Basically you want to set constraints for each tableview to the edges which they are touching and their neighboring tableviews (distance: 0). Furthermore you have to set constraints for equal width and equal heights.

Related

Why are my cells too small when I run my code and visualise my user interface?

I designed my Table View with a customised cell. I set the height of my custom cells to 350 but when I run my code and visualise my user interface, only the image of my cell can be seen and not fully ! I cannot visualise my whole cell, it makes up 1/10 of my table view. Can anyone tell me how to fix that?
I set the height of my custom cells to 350
The height of the cell as shown in the storyboard or xib doesn't matter. It is ignored!
What's important is what the table thinks the cell height is. You can set this in several ways:
Set the table view's rowHeight to 350. But that sets the height of all the cells.
Or, return 350 from your implementation of heightForRowAt for these cells.
Or, give the table view only an estimated row height, but then your cells must have sufficient internal autolayout constraints top to bottom to determine completely and unambiguously the height.

Letting a custom view grow with a MultiLineLabel subview

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.

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 Autosize (Vertically)

Is it possible to have an NSView autosize (vertically) to fit its contents? The contents are just two NSTextFields, one (on top of the view) that's always the same height and the other (near the bottom of the view) that's of a variable height.
Something that could make it more difficult is that the NSView is an NSCollectionViewItem's view.
The problem is that the width needs to be known before the height can be calculated. A successful implementation requires two layout loops. The first determines the width. That width is then taken and the height is calculated. A constraint is then added or modified to reflect the height and layout must happen again.
To achieve this, I subclassed the view to store a copy of the frame size locally. When the contents of the view changed, I would clear the width to zero. If the actual frame rectangle width was discovered to be different than the stored width after layout, then I knew that height needed calculated and another layout needed to be executed in the event that the width change caused a height change.

Resources