7 模态呈现
6 分钟阅读
原文链接: https://developer.apple.com/documentation/swiftui/modal-presentations
7 模态呈现
在单独的视图中呈现内容,提供聚焦的交互。
概述
为了聚焦于一个重要的、范围有限的任务,你会显示一种模态呈现,例如提醒框、弹出视图(popover)、表单(sheet)或确认对话框。

在 SwiftUI 中,你用一个视图修饰符来创建模态呈现,该修饰符定义了呈现的外观,以及 SwiftUI 在什么条件下呈现它。SwiftUI 会检测条件的变化,并为你完成呈现。由于你为触发呈现的条件提供了一个 Binding,SwiftUI 就能在用户关闭呈现时重置底层值。
关于设计指导,参见 Human Interface Guidelines 中的模态。
配置对话框
- DialogSeverity —— 提醒框或确认对话框的严重程度。
显示表单、全屏封面或弹出视图
- sheet(isPresented:onDismiss:content:) —— 当你提供的布尔值绑定为 true 时呈现一个表单。
- sheet(item:onDismiss:content:) —— 用给定条目作为表单内容的数据源来呈现表单。
- fullScreenCover(isPresented:onDismiss:content:) —— 当你提供的布尔值绑定为 true 时,呈现一个尽可能覆盖整个屏幕的模态视图。
- fullScreenCover(item:onDismiss:content:) —— 用你提供的绑定作为表单内容的数据源,呈现一个尽可能覆盖整个屏幕的模态视图。
- popover(item:attachmentAnchor:arrowEdge:content:) —— 用给定条目作为弹出视图内容的数据源来呈现弹出视图。
- popover(isPresented:attachmentAnchor:arrowEdge:content:) —— 当给定条件为 true 时呈现弹出视图。
- PopoverAttachmentAnchor —— 弹出视图的附着锚点。
适配呈现大小
- presentationCompactAdaptation(horizontal:vertical:) —— 指定呈现如何适配水平与垂直紧凑尺寸类别。
- presentationCompactAdaptation(_:) —— 指定呈现如何适配紧凑尺寸类别。
- PresentationAdaptation —— 把呈现适配到不同尺寸类别的策略。
- presentationSizing(_:) —— 设置所在呈现的大小。
- PresentationSizing —— 定义呈现内容大小的类型,以及呈现大小如何随其内容大小变化而调整。
- PresentationSizingRoot —— 提供给呈现、且带有已定义呈现大小的视图代理。
- PresentationSizingContext —— 关于某个呈现的上下文信息。
配置表单的高度与放置
- presentationDetents(_:) —— 设置所在表单可用的停靠高度。
- presentationDetents(_:selection:) —— 设置所在表单可用的停靠高度,并让你以编程方式控制当前选中的停靠高度。
- presentationContentInteraction(_:) —— 配置呈现上轻扫手势的行为。
- presentationDragIndicator(_:) —— 设置表单顶部拖动指示器的可见性。
- PresentationDetent —— 表示表单自然停留高度的类型。
- CustomPresentationDetent —— 带计算高度的自定义停靠高度定义。
- PresentationContentInteraction —— 可用来影响呈现如何响应轻扫手势的行为。
- presentationPlacement(_:) —— 设置呈现相对呈现它的视图的放置位置。
- PresentationPlacement —— 呈现相对呈现它的视图的放置位置。
设置表单及其背景的样式
- presentationCornerRadius(_:) —— 请求呈现使用指定的圆角半径。
- presentationBackground(_:) —— 用形状样式设置所在表单的呈现背景。
- presentationBackground(alignment:content:) —— 把所在表单的呈现背景设置为自定义视图。
- presentationBackgroundInteraction(_:) —— 控制人们能否与呈现背后的视图交互。
- PresentationBackgroundInteraction —— 呈现背后的视图可用的交互种类。
呈现提醒框
- AlertScene —— 把自身渲染为独立提醒对话框的场景。
- alert(_:isPresented:actions:) —— 当给定条件为 true 时呈现提醒框,标题使用本地化字符串资源。
- alert(_:isPresented:presenting:actions:) —— 用给定数据生成提醒框内容、并用本地化字符串资源作为标题来呈现提醒框。
- alert(_:item:actions:) —— 用给定数据生成提醒框内容、并用文本视图作为标题来呈现提醒框。
- alert(error:actions:) —— 当存在错误时呈现提醒框。
- alert(isPresented:error:actions:) —— 当存在错误时呈现提醒框。
- alert(_:isPresented:actions:message:) —— 当给定条件为 true 时呈现带消息的提醒框,标题使用本地化字符串资源。
- alert(_:isPresented:presenting:actions:message:) —— 用给定数据生成提醒框内容、并用本地化字符串资源作为标题,呈现带消息的提醒框。
- alert(_:item:actions:message:) —— 用给定数据生成提醒框内容、并用本地化字符串键作为标题,呈现带消息的提醒框。
- alert(error:actions:message:) —— 当存在错误时呈现带消息的提醒框。
- alert(isPresented:error:actions:message:) —— 当存在错误时呈现带消息的提醒框。
为操作获取确认
- confirmationDialog(_:isPresented:titleVisibility:actions:) —— 当给定条件为 true 时呈现确认对话框,标题使用本地化字符串资源。
- confirmationDialog(_:isPresented:titleVisibility:presenting:actions:) —— 用数据生成对话框内容、并用本地化字符串资源作为标题来呈现确认对话框。
- dismissalConfirmationDialog(_:shouldPresent:actions:) —— 当关闭动作被触发时呈现确认对话框。
显示带消息的确认对话框
- confirmationDialog(_:isPresented:titleVisibility:actions:message:) —— 当给定条件为 true 时呈现带消息的确认对话框,标题使用本地化字符串资源。
- confirmationDialog(_:isPresented:titleVisibility:presenting:actions:message:) —— 用数据生成对话框内容、并用本地化字符串资源作为标题,呈现带消息的确认对话框。
- dismissalConfirmationDialog(_:shouldPresent:actions:message:) —— 当关闭动作被触发时呈现确认对话框。
配置对话框
- dialogIcon(_:) —— 配置该视图内对话框所使用的图标。
- dialogIcon(_:) —— 配置提醒框所使用的图标。
- dialogSeverity(_:)
- dialogSeverity(_:) —— 设置提醒框的严重程度。
- dialogSuppressionToggle(isSuppressed:) —— 让用户可以抑制在
self内呈现的对话框和提醒框,在 macOS 上带默认的抑制消息。其他平台不使用。 - dialogSuppressionToggle(isSuppressed:) —— 让用户可以用自定义的抑制消息抑制提醒框。
- dialogSuppressionToggle(_:isSuppressed:) —— 让用户可以抑制在
self内呈现的对话框和提醒框,在 macOS 上带自定义的抑制消息。其他平台不使用。 - dialogSuppressionToggle(_:isSuppressed:) —— 让用户可以用自定义的抑制消息抑制提醒框。
- dialogPreventsAppTermination(_:) —— 提醒框或确认对话框是否阻止系统或应用退出菜单项终止应用。
导出到文件
- fileExporter(isPresented:document:contentType:defaultFilename:onCompletion:onCancellation:) —— 呈现系统对话框,允许用户把某个
WritableDocument导出到磁盘上的文件。 - fileExporter(isPresented:documents:contentTypes:onCompletion:onCancellation:) —— 呈现系统对话框,允许用户把一组遵循
WritableDocument的对象导出到磁盘上的文件。 - fileExporter(isPresented:item:contentTypes:defaultFilename:onCompletion:onCancellation:) —— 呈现系统对话框,允许用户把某个
Transferable条目导出到磁盘上的文件。 - fileExporter(isPresented:items:contentTypes:onCompletion:onCancellation:) —— 呈现系统对话框,允许用户把一组
Transferable条目导出到磁盘上的文件。 - fileExporterFilenameLabel(_:) —— 在 macOS 上,为
fileExporter配置文件名输入框的标签。
从文件导入
- fileImporter(isPresented:allowedContentTypes:allowsMultipleSelection:onCompletion:) —— 呈现系统对话框,允许用户导入多个文件。
- fileImporter(isPresented:allowedContentTypes:onCompletion:) —— 呈现系统对话框,允许用户导入一个已有文件。
- fileImporter(isPresented:allowedContentTypes:allowsMultipleSelection:onCompletion:onCancellation:) —— 呈现系统对话框,允许用户导入多个文件。
移动文件
- fileMover(isPresented:file:onCompletion:) —— 呈现系统对话框,允许用户把已有文件移动到新位置。
- fileMover(isPresented:files:onCompletion:) —— 呈现系统对话框,允许用户把一组已有文件移动到新位置。
- fileMover(isPresented:file:onCompletion:onCancellation:) —— 呈现系统对话框,允许用户把已有文件移动到新位置。
- fileMover(isPresented:files:onCompletion:onCancellation:) —— 呈现系统对话框,允许用户把一组已有文件移动到新位置。
配置文件对话框
- fileDialogBrowserOptions(_:) —— 在 macOS 上,配置
fileExporter、fileImporter或fileMover,提供更精细的 URL 搜索体验:包含或排除隐藏文件、允许按标签搜索等。 - fileDialogConfirmationLabel(_:) —— 在 macOS 上,为
fileExporter、fileImporter或fileMover配置自定义的确认按钮标签。 - fileDialogCustomizationID(_:) —— 在 macOS 上,配置
fileExporter、fileImporter或fileMover以持久保存并恢复文件对话框配置。 - fileDialogDefaultDirectory(_:) —— 配置
fileExporter、fileImporter或fileMover以指定的默认目录打开。 - fileDialogImportsUnresolvedAliases(_:) —— 在 macOS 上,配置用户选择别名时
fileExporter、fileImporter或fileMover的行为。 - fileDialogMessage(_:) —— 在 macOS 上,为
fileExporter、fileImporter或fileMover配置一条呈现给用户的自定义消息,类似标题。 - fileDialogURLEnabled(_:) —— 在 macOS 上,配置
fileImporter或fileMover有条件地禁用所呈现的 URL。 - FileDialogBrowserOptions —— 文件对话框呈现文件系统的方式。
呈现检查器
- inspector(isPresented:content:) —— 在视图层级中应用修饰符的位置插入一个检查器。
- inspectorColumnWidth(_:) —— 当检查器作为后置栏呈现时,为包含该视图的检查器设置固定的偏好宽度。
- inspectorColumnWidth(min:ideal:max:) —— 在后置栏呈现中为检查器设置灵活的偏好宽度。
关闭呈现
- isPresented —— 表示与该环境关联的视图当前是否已呈现的布尔值。
- dismiss —— 关闭当前呈现的动作。
- DismissAction —— 关闭呈现的动作。
- interactiveDismissDisabled(_:) —— 有条件地阻止通过交互关闭弹出视图、表单和检查器等呈现。
已弃用
- Alert —— 提醒框呈现的表示形式。
- ActionSheet —— 操作表单呈现的表示形式。
- fileExporter(isPresented:document:contentType:defaultFilename:onCompletion:) —— 呈现系统对话框,用于把存储在值类型(如结构体)中的文稿导出到磁盘上的文件。
- fileExporter(isPresented:documents:contentType:onCompletion:) —— 呈现系统对话框,用于把一组值类型文稿导出到磁盘上的文件。
- fileExporter(isPresented:document:contentTypes:defaultFilename:onCompletion:onCancellation:) —— 呈现系统对话框,允许用户把某个
FileDocument导出到磁盘上的文件。