1

I'm building a simple NSGridView, and want to have a custom NSView as each element of the grid. Eventually, each NSView will be a xib based label (NSTextField) centered in the NSView. The problem I am having is with the intrinsic size of the NSView. I want to define the size of the NSView and have auto layout work based on that. I added this code to the custom view (labelView):

override var intrinsicContentSize: NSSize { return NSSize(width:100, height:100); }; 

And it is indeed called; but apparently ignored. As a test, I have on the same row some other labels, and the height for the row is always set to the largest of the row text heights (including the label in the custom view); but the length is set to the longest of column text fields, ignoring the label in the custom view. And anyway, I want to arbitrarily make the NSView a certain height and length, as I tried (but failed) to do with the intrinsicContentSize.

NSStackview seems to do the right thing; but NSGridView does not.

I can force the width of a particular column with

 grid.column(at:0).width = 400; 

but want I really want to do is define the size of the NSView, and let autolayout use that as a building block.

This strikes me as a conceptual error on my part, so if someone could explain these NSGridView-autolayout-NSView subtleties, I think many might benefit.

7
  • you are fresh to swift.. you dont need the ;, did you try to grid.invalidateIntrinsicContentSize ? Commented Jul 31, 2020 at 2:59
  • I did, but I may not have been using it correctly. What I have found is that the width is respected in intrinsicContentSize, but not the height; and the label.heightAnchor is respected but not the label.widthAnchor: Commented Aug 1, 2020 at 20:53
  • override var intrinsicContentSize: NSSize{ invalidateIntrinsicContentSize() return NSSize(width:390,height: 200) // 200 can be anything, no change }; mainLabel.widthAnchor.constraint(equalToConstant: 430.0).isActive = true // does nothing Commented Aug 1, 2020 at 21:01
  • just had a read up again about grid views, maybe leading you in a wrong direction. Dont you actually define NSGridRow height explicitly and NSGridColumn width explicitly, which defines the Cells size where you UIView is nested in? Or are you trying to stick with the idea that you can have a CollectionView just like on iOS? Commented Aug 1, 2020 at 21:50
  • What is yPlacement of the grid view? Commented Aug 2, 2020 at 15:08

1 Answer 1

0

I was having the exact same issue, tried to use custom NSView's inside a NSGridView and couldn't get them to draw correctly. What finally worked for me was setting the following:

let gridSize = 5 let cellSize: CGFloat = 50 gridView.xPlacement = .fill // this was key part of the solution gridView.yPlacement = .fill for i in 0 ..< gridSize { gridView.row(at: i).height = cellSize gridView.column(at: i).width = cellSize } 

Note that I'm setting the size of each cell with the row height and column width of the NSGridView, and not using the NSView size, but this is the only way I got it working.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.