NSView Autosize (Vertically) - macos

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.

Related

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

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.

AutoLayout contraints - cannot resize height

I'm struggling a bit with the auto layout constraints in a super simple test view.
For test I added a label, want it x number of pixels from left side, and 5 pixels from top, right, bottom.
When I do this and do an update frame, the view collapse to the min height, and when I insert the view into a tab, the main window collapses and I cannot resize the height.
I can understand why the view collapses to min height, but why does the main window collapse, and why can't I adjust the height.
Which is the missing / wrong constraints.
Thanks
Edit: Noticed I by mistake used a TextField instead of a Label which could have explained the fixed height, but even after changing to a label I see the same issue. If I instead set the height to fixed, so the constraints are left, top, right, height, then I can resize the window as expected, but obviously the label doesn't resize its height.
The label's content-hugging priorities are presumably greater than NSLayoutPriorityWindowSizeStayPut (500). That tells the auto layout system to prefer to resize the window rather than stretch the label beyond the size required by its contents.
Reduce that priority.

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.

Vertical scrolling UIScrollview with Imageview and textview

I spent last 2 days trying to figure out UIScrollview using storyboard. Whenever I think I got it, a new problem appears and now it seams unsolvable.
Here is what I'm trying to do (it's really simple):
I have an Imageview 400x185 taking the whole width of the scrollview (edge to edge). That means that the scrollview has the width of the imageview.
Below is a textview that has <= width of the imageview.
I used many methods described on stackoverflow:
- using a view on the scrollview and setting the width same as the parent of the scrollview.
- using just the scrollview without a view
All of them produced errors in the frame size of the image, size of the subview of scrollview, constrains, textview doesn't show up or the picture is too large even though I set the constrains edge to edge.
Can someone make a sample project with the UI described above? It would take 5 minutes for someone who knows how to deal with it.
I just did this. I know it works. Here is what I would suggest:
Make sure your 400x600 view is constrained to width 400 and height 600.
Add a scroll view to this view. Have it take up the full view and then pin top, bottom, left, and right.
Add a UIView to the scroll view. Have it take up the full view and then pin top, bottom, left, and right. This is your content view. I renamed my in the document outline to "ContentView". At this point, you will have a warning about ambiguous content size, because until you add more constraints, the content view can grow to any size.
The size of the content view will be determined by the contents, so they need to be fully constrained. Add the imageView to the content view. Constrain its width to 400, constrain its height to 185. Pin it to the left, top, and right of the contentView. At this point, the contentView will know it is 400 wide, but it still doesn't know how tall it is.
Add the textView to the contentView. Pin it a constant distance from the imageView. Constrain it to be centered in its contentView. Pin it a constant distance from the bottom of the contentView. Constrain its width to something less than 400 and its height to something big like 600.
At this point, the size of your contentView should be fully constrained. It gets it width from the width of the imageView which is 400 wide and pinned to both sides of the contentView. It gets its height from the constant distance of the imageView from the top + the height of the imageView + the constant distance between the imageView and the textView + the height of the textView + the constant distance from the textView to the bottom of the contentView.
If you want your scrollView to only scroll vertically, then constrain the contentView width to the scrollView width. This is easily done in the Document Outline view by control dragging from the contentView to the scrollView and then selecting Equal Widths from the pop up.

Resources