Use .contextMenu on tvOS

Daily Coding Tip 048

Like .tabItem which was mentioned yesterday, .contextMenu was new last year but has come to a new platform this year, and like .tabItem, I've used one of Apple’s examples.

This time I put in a little more effort, as adapting their example for tvOS actually requires that the Text be made .focusable, which is covered in detail earlier in this post. Once a View can become the focused element on the screen, it can also receive a long press gesture. If it has a .contextMenu modifier, this brings up the list of options you provided to it.

The fact that I was able to use a Text means that you don’t need a Button to do this, but as I said the View you use must use the .focusable() modifier for this to work.

Here’s how I did it.


Get more Daily Coding Tips in your inbox!