17

I am looking at this SwiftUI tutorial and it suggests I can see extra actions upon Command-Clicking the SwiftUI elements in code. In my Xcode, this jumps me to the class definition.

enter image description here

I see there's an option in XCode 11.0 t change the Option key to show SwiftUI inspector, however, this still does not let me modify SwiftUI code elements using actions like "Embed in VStack"

How can I get both inspector and extra SwiftUI actions when interacting with SwiftUI Code (not the preview)?

enter image description here

enter image description here

sandpat
  • 1,478
  • 12
  • 30
Alex Stone
  • 46,408
  • 55
  • 231
  • 407
  • 5
    The Canvas must be opened, please look [here](https://stackoverflow.com/a/58888687/12299030) for details. – Asperi Jan 01 '20 at 13:01

5 Answers5

38

Extra SwiftUI actions show when Canvas is opened (shortcut: Option-Command-Enter or ⌥+⌘+↵):

when canvas close canvas is closed

when canvas is open canvas is opened

Hrabovskyi Oleksandr
  • 3,070
  • 2
  • 17
  • 36
13

The accepted answer doesn't work for me on macOS 10.15.5 and Xcode 11.4.1.

The Good news is that you can get it via a shortcut of [Command][Shift][Left Click] on a SwiftUI element or by clicking Show Code Actions on the right click menu.

Right click > Show Code Actions

Tristan Warner-Smith
  • 9,631
  • 6
  • 46
  • 75
  • 2
    This was helpful to me as I had the same problem. Then, I found that if I go to Xcode -> Preferences -> Navigate and change the command click behavior to Selects Code Structure, I can use command+click to get there too. – charmingToad Aug 27 '20 at 04:27
8

Instead of using the mouse, I recommend using the Xcode hotkey for bringing up code actions. Navigate to Preferences -> Key Bindings and search for Show Code Actions. You'll see the preset hotkey which you can then customize. My default key binding was Shift+Command+A. Just make sure you cursor is over the intended element when you press the hotkey.

enter image description here

Nathan Dudley
  • 510
  • 7
  • 17
  • This works on Xcode 13.4 (CMD+SHIFT+A) – Suhail Doshi May 30 '22 at 18:15
  • Nathan, do you know if there's a way to set a shortcut for a specific code action? – zh. Jun 22 '22 at 08:26
  • 1
    @zh Some of the code actions are bindable such as "Jump to Definition" and "Show SwiftUI Inspector" but from what I can tell Xcode does not yet support shortcuts for things like "Embed in HStack" etc. I would love to be wrong about this. – Nathan Dudley Jun 27 '22 at 15:59
4

You can change the command click behavior where you were looking under Xcode -> Preferences, Navigate tab.

enter image description here

Rather unintuitively, if you pick "Selects Code Structure", then command+click will bring up the SwiftUI menu options you're looking for (granted the canvas is open).

If you do this, you can still jump to definition via control+command+click

Conversely, if you'd prefer to continue to use command+click to jump to definition, you can instead use control+command+click to bring up the SwiftUI menu options.

charmingToad
  • 1,597
  • 11
  • 18
0

Command clicking on a view will not work in Xcode 12.2 and 12.3 (under MacOS 10.15.5) if you have your preferences different than Xcode expects.

You have to change your Navigation preferences for Command-click on Code: to Selects Code Structure before the proper menu will appear when you command click on a View.

To change this. press command , to open your preferences and select the Navigation button. The option you need to change is in the first popup menu.

Alex Zavatone
  • 4,106
  • 36
  • 54