如何在Xcode中折叠所有方法?

236

如何在Xcode中折叠一个类中的所有方法?

逐个折叠不再是一种选择。


您可以在此处下载免费的Xcode快捷键备忘单。http://www.pragprog.com/screencasts/v-mcxcode/becoming-productive-in-xcode - falconcreek
@falconcreek,不幸的是,十年后它已经失效了 :( - hamsternik
7个回答

495

Xcode 4版本以后似乎已经发生了改变。

command-alt-shift-left arrow可以解决问题...

折叠/展开当前方法或if结构使用:

折叠:command-alt-left arrow

展开:command-alt-right arrow


35
同时,按下Command + Option + 左箭头将折叠当前块。使用相同的组合键和右箭头展开。 - Omer
2
不错的技巧!这将为我节省很多时间。它在Xcode 6.1.1中运行。 - Cesare
太棒了伙计。这正是我在寻找的。谢谢。 - Abdul Yasin
太棒了...我的第一个Kinesis宏。 - KinGBin

141

Xcode 10中的更新

Xcode 10增加了对代码折叠的支持,包括:

  1. 一个新的代码折叠功能条,在编辑器中显示所有可折叠的多行代码块
  2. 编辑器中折叠代码的新样式,允许您编辑折叠代码的行
  3. 支持折叠由花括号括起来的任何代码块
  4. 支持从折叠功能条、结构化选择或菜单栏中折叠代码块

菜单栏 ► 编辑器 ► 代码折叠 ► 折叠菜单项

enter image description here

看这张快照:

enter image description here


在 Xcode 9 beta 1 中禁用了代码折叠功能,现在已经修复,在 Xcode 9 Beta5 中根据测试版发布说明:Xcode 9 beta 5 – IDE

这是如何操作的:

  1. 按住键盘上的 (命令)按钮并将鼠标光标移动/悬停在任何一对大括号(开始或结束)。它会自动高亮显示,块区域。
  2. 保持 (命令)按钮处于按下状态,单击高亮区域。它将启用快速菜单弹出窗口,其中包含Fold选项。
  3. 从菜单列表中选择Fold。它将折叠您的代码并显示3个点,覆盖整个块。
  4. 现在,要再次展开您的代码块,请释放 (命令)按钮并单击折叠的块上的3个点。

为了更容易理解,看一下这个快照:

enter image description here


所有的键盘快捷键也都能正常工作。
Fold                          ⌥ ⌘ ←      option + command + left arrow
Unfold                        ⌥ ⌘ →      option + command + right arrow
Unfold All                    ⌥ U        option + U
Fold Methods & Functions      ⌥ ⌘ ↑      option + command + up arrow
Unfold Methods & Functions    ⌥ ⌘ ↓      option + command + down arrow
Fold Comment Blocks           ⌃ ⇧ ⌘ ↑    control + shift + command + up
Unfold Comment Blocks         ⌃ ⇧ ⌘ ↓    control + shift + command + down
Focus Follows Selection       ⌃ ⌥ ⌘ F    control + option + command + F
Fold All                      ⌘ ⌥ ⇧ ←    command + option + shift + left
Unfold All                    ⌘ ⌥ ⇧ →    command + option + shift + right

菜单栏 ▶ 编辑器 ▶ 代码折叠 ▶ "这里是代码折叠选项列表"

这是参考快照:

enter image description here

来自Xcode快捷键列表的相同选项:

菜单栏 ▶ Xcode ▶ 首选项 ▶ 键绑定 ▶ "这里是代码折叠快捷键列表"

enter image description here


我发现代码折叠在尝试通过单击省略号展开时存在错误。只有10次中的7次才能正确工作。其他时候,我必须从编辑器菜单选项中展开所有内容。 - Micah Montoya
不确定我做错了什么,但是命令选项Shift左键不能折叠全部。 - João Serra
“Unfold All” 在您的快捷方式列表中出现了两次。 - TT--
这种方法在Xcode 13中也适用。 - mitsu

22

X-Code 7 及以上版本

折叠所有方法:按下 Shift + Option + Command + 左箭头

展开所有方法:按下 Shift + Option + Command + 右箭头


如果我只想要一个方法怎么办? - Marin

11

我喜欢Krunal的答案中的这张表(上面):https://dev59.com/IXE85IYBdhLWcg3wUB2z#46020397

Fold                          ⌥ ⌘ ←      option + command + left arrow
Unfold                        ⌥ ⌘ →      option + command + right arrow
Fold Methods & Functions      ⌥ ⌘ ↑      option + command + up arrow
Unfold Methods & Functions    ⌥ ⌘ ↓      option + command + down arrow
Fold Comment Blocks           ⌃ ⇧ ⌘ ↑    control + shift + command + up
Unfold Comment Blocks         ⌃ ⇧ ⌘ ↓    control + shift + command + down
Focus Follows Selection       ⌃ ⌥ ⌘ F    control + option + command + F
Fold All                      ⌘ ⌥ ⇧ ←    command + option + shift + left
Unfold All                    ⌘ ⌥ ⇧ →    command + option + shift + right

9
看起来是 Ctrl-Command-Up(或在菜单上选择:Editor-CodeFolding-FoldMethods/Functions)可以折叠方法/函数。
使用 Ctrl-Command-Down 或者 Ctrl-U 可以撤销折叠。

2
  1. 按下 COMMAND + , "command comma"

  2. 点击 "文本编辑" 选项卡

  3. 勾选 "代码折叠带"

在行号的右侧,您将看到一个新的带有向上或向下箭头的带子,旁边是函数/方法。按下这些箭头将允许您展开或折叠函数/方法。


0
Shift + Option + Command + 左箭头对我没有起作用。 我找到了一个解决方法,如果使用“查找”并输入“[”。 使用“回车”跳转到数组。当你来到一个你想关闭的数组时,使用“Option + Command + 左箭头”。 你可以很快地关闭很多数组,而不需要点击任何东西。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接