How to get the height of an NSTableCellView that uses autolayout - macos

I have a view-based NSTableView. I positioned the textField and other elements in an NSTableCellView in Interface Builder with autolayout. How can I get the height for the cell based on its autolayout parameters?
This answer is the closest I have found to what I am looking for, but it is written for iOS and the systemLayoutSizeFittingSize method does not exist on NSView.

You could put the views inside your table cell view into a separate NSView (let's call this view contentView). Set Auto Layout constraints for the contentView so that it aligns to its parent view at the left (leading), top and right (trailing). Make sure the subviews of contentView resize their parent view. Create an IBOutlet on that view in your NSTableCellView subclass. To calculate the height, call fittingSize on contentView which should get you the minimal size of your subviews based on the constraints.

Related

How Can I Make a View, With a Tab View in It, Scrollable?

I got an NSTabView inside an NSView. That NSView is in an NSClipView, which in turn is in an NSScrollView. It looks like this (NSTabView in green, and NSView in red):
As you can see, the content of NSTabView gets clipped, and no scrollbars appear (since the view doesn't expand beyond the window).
How can I make NSTabView take up as much space as it needs (doesn't clip out), and expand the NSView with it? Then, NSScrollView can deal with the scrolling of the overgrown NSView.
Since my content changes dynamically, I don't want to put in some hard values for the width and height of NSTabView's superview.
This is only part of it; here's now the overall hierarchy looks:
I want the NSTabView's superview to be scrollable instead of clipping out, like this:
The setup I'll describe is for an NSTabView that will pin to the top, left, and right sides of the scroll view. Note the NSTabView could be replaced with any other NSView, the setup is the same.
Starting with you putting a scroll view into the xib/storyboard, you'll have NSScrollView -> NSClipView -> NSView (document view). Constrain the NSScrollView to the edges of the window. Drop your NSTabView onto the NSView instance. Add constraints so that your NSTabView edge constraints equal the NSView and define a height constraint either explicitly or implicitly with other content inside the tab view that defines it.
Personally I like to change the NSView instance (document view) layout to use constraints, by default it uses autoresizing masks and this makes it difficult to keep it in sync with the NSTabView. We want the document view to be pinned to the top, left, and right sides of the scroll view. The size of this view is what determines the scrollable region so we want it to be the same size as the NSTabView so the height of the tab view will determine the scrollable area.
To change this, select the document view, and under the Size Inspector we want to change the "Layout" type to "Automatic".
Lastly, add constraints to the top, left, and right and you should be good to go.
If you want the scroll view to start at the top rather than the bottom, you should subclass the document view and override isFlipped:
class FlippedView: NSView {
override var isFlipped: Bool { true }
}

Custom NSView embedded in NSSscrollView

Is there a way for a custom NSView to know whether it is embedded in a NSScrollView or not?
I am creating a custom NSView for displaying some content.
When my view is placed in a window or another view, its size is fixed and the content is clipped to available size.
When my view is placed in a NSScrollView its size must be adjusted according to content so it can be scrolled if necessary.
I know I can add a member in my view that specifies the NSScrollView that hosts my view and set this member manually in code, but I was wondering if there is another way?
You didn't check the methods of NSView?
#property(readonly, strong) NSScrollView *enclosingScrollView;
or
var enclosingScrollView: NSScrollView? { get }
The nearest ancestor scroll view that contains the current view.
If the current view is not embedded inside a scroll view, the value of this property is nil. This property does not contain the current view if the current view is itself a scroll view. It always contains an ancestor scroll view.

How to dynamic height UIView on UITablewView

I have UITableViewController and I insert UIView on top with elements in view. I get problem with height of UIView. All UIlabel are multiline 0 for dynamic height. I want to make UIView height flexible with respect to UILabel in view (by attach image below). I have made it using storyboard and set autolayout but doesn't work for me. Can I make it ? Thank a lot!

Getting the preferred size of an NSCollectionView

I'd like to center an NSCollectionView in its enclosing scroll view (horizontally & vertically).
How can I determine the ideal size of the collection view so that all item views fit?
It appears that a collection view will always resize itself to fill the entire document view when enclosed in a scroll view.
Also, if not enclosed in a scroll view, the collection view won't change its frame when new items are added.
I would suggest you to customise the NSCollection View. All you can do is override "drawRect" Method of NSCollectionView and inset its own rect. Below is what will fix your issue
Steps to implement:
Create a Class "NSModifiedCollectionView" Where it inherits from NSCollection View and do the following.
#interface NSModifiedCollectionView : NSCollectionView
#end
#implementation NSModifiedCollectionView
- (void) drawRect:(NSRect)dirtyRect
{
//You can inset more than 9.0 as per your needs but this will make you fit exactly
NSRect insetRec = NSInsetRect(dirtyRect, 9.0, 9.0);
[super drawRect:insetRec];}
2: Drag and Drop a NSCollectionView from you object list to the Xib and modify its custom class to "NSModifiedCollectionView". Now Run and check it should work as you expect.

How to set the width of the view displayed as a row in a NSTableView?

I have a view-based NSTableView.
I have created a NSView to insert in this NSTableView as a row.
In the xib defining the NSView, the latter has a width set to 280.
When I add my NSView to my NSTableView, the width of the former is changed to be equal to the width of the NSTableView.
Is there a nice way to prevent this behavior ?
I thought of including my NSView in another one, but I feel it is a bit hacky.
If possible, I would like a solution that only involves settings of the NSTableView. This NSTableView is indeed to be populated with many differents NSViews.
As I mentioned in my comment to your previous question. You want to add your own views as subviews of NSTableCellViews. The NSTableCellView will be set to the width of the table, but your child view can be any width you like.
this might be naive, but what happens if you uncheck Autoresizes Subviews in TableView's Attribute Inspector?

Resources