1

I am creating an app for macOS using XIB and Swift, XCode 12.5. What I would like to achieve is a window with two NSTableViews side-by-side, having equal widths, filling the available space. A Stack View seems the obvious choice here.

I am easily able to use constraints to make a single NSTableView fill a window and resize correctly, but my difficulty is having two side-by-side NSTableViews. I've tried everything I can think of, but the symptom is always roughly the same: in Interface Builder, expanding the window, the NSTableViews grow (equally, correctly) but shrinking the window they remain at their previous maximum size.

failed attempt at constraints

Running this app gives a different result: the tables do actually resize correctly, but the cell sizes are out of whack. These tables are (should be) identical copies of one another: I literally copy-pasted the second one.

at runtime, the tables don't look 100% the same

I have searched for information about using NSTableView inside Stack Views, but have come up empty-handed. I have probably overlooked something! I should add that I'm happy to move to Storyboard or even SwiftUI if it means being able to solve my problem, but it looks as if SwiftUI and NSTableView will complicate the matter.

I have tried to show my work here: https://github.com/toothbrush/tabletest2.

RogerTheDragon
  • 267
  • 1
  • 10
  • You don't need a stack view, just add constraints to stick the scroll views to the sides and have equal widths. – Willeke Jun 02 '21 at 06:47
  • I just tried to remove the Stack View and do as you say, but I must be doing it wrong. Both tables are showing an error, "Need constraints for: X position or width", while those should be present. I'll push my example project to Github. – RogerTheDragon Jun 02 '21 at 07:07
  • I realise that Interface Builder wasn't wrong about that. I subsequently tried to pin the "inner" edges of the NSTableViews to the Superview's CenterX, but that sometimes crashes IB, but perhaps it's making progress! – RogerTheDragon Jun 02 '21 at 07:33
  • Interesting – the crash occurs when I try to use "Standard" offset to the CenterX of the Superview. I guess I will just have to hard-code an offset there. – RogerTheDragon Jun 02 '21 at 07:34
  • Alright, now the only thing I don't understand is why it is all fine when running the app, but in Interface Builder when I drag and resize the window, it just gives lots of "misplaced view" warnings and doesn't reapply the constraints. Is this expected behaviour? – RogerTheDragon Jun 02 '21 at 07:45
  • I forgot to mention the spacing constraint between the scroll views. Xcode is buggy. Avoid resizing the window and/or solve layout issues by updating the frames (in Xcode 9: menu Editor -> Resolve Layout Issues -> Update Frames). – Willeke Jun 02 '21 at 08:19
  • Sorry about the delay @Willeke, I have mostly succeeded, but I still have issues with "Update Frames". I have posed a new question here: https://stackoverflow.com/questions/68013147/struggles-with-side-by-side-nstableview-and-contraint-layout-part-2. I'm sorry to bother you about it again; I wonder if you see anything obviously wrong? – RogerTheDragon Jun 17 '21 at 04:56

0 Answers0