NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Using a custom NSView inside an NSGridView

Forums > macOS


I'm working my way through Hacking with macOS, and recently started messing around with NSGridView but I have an issue that's driving me crazy. When I create a new NSGridView with NSGridView(views: [[NSButtons]]) as per the "Odd one out" game it works fine and the buttons are displayed. However, I want to have my own custom views inside an NSGridView like NSGridView(views: [[MyCustomView]]) but I can't get it to display anything.

I guess I'm missing something trivial here like setting the height and width or something. I do as in the "Odd one out" game, like setting constraints and row height and column width on the grid view though. My custom view only draws a CGRect 20x20 right now, so that should give it a size, I guess?

Anyone see anything obvious that I have missed? :)

Also, my end goal is a pixel editor for sprites, so I'm not sure the NSGridView is the best option here. Maybe I should just do a custom NSView and "paint" the grid myself, and figure out mouse coordinates etc. in code. Any opinions on this?

Thanks for your attention :)

Regards, Andreas


Hacking with Swift is sponsored by MadMachine

SPONSORED Want to explore your Swift skill outside of the Apple world? Join the MadMachine community and start to program microcontrollers in Swift.

Get it now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.