1.14 呈现修饰符
7 分钟阅读
原文链接: https://developer.apple.com/documentation/swiftui/view-presentation
1.14 呈现修饰符
定义视图在指定条件下要呈现的附加视图。
概述
使用呈现修饰符可以显示不同类型的模态呈现,例如提醒、浮层、工作表和确认对话框。
由于 SwiftUI 是声明式框架,你不需要在想呈现模态视图的那一刻调用某个方法。相反,你定义呈现的样子,以及 SwiftUI 应在什么条件下呈现它。SwiftUI 会检测该条件何时变化,并替你完成呈现。由于你为触发呈现的条件提供了一个 Binding,当用户关闭呈现时,SwiftUI 可以重置底层值。
关于如何使用这些修饰符的更多信息,请参阅 模态呈现。
提醒
- 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:) — 使用数据生成对话框内容,并使用本地化字符串资源作为标题来呈现一个确认对话框。
- confirmationDialog(_:item:titleVisibility:actions:) — 使用数据生成对话框内容,并使用文本视图作为标题来呈现一个确认对话框。
- dismissalConfirmationDialog(_:shouldPresent:actions:) — 当关闭动作已被触发时呈现一个确认对话框。
带消息的确认对话框
- confirmationDialog(_:isPresented:titleVisibility:actions:message:) — 当给定条件为 true 时呈现一个带消息的确认对话框,并使用本地化字符串资源作为标题。
- confirmationDialog(_:isPresented:titleVisibility:presenting:actions:message:) — 使用数据生成对话框内容,并使用本地化字符串资源作为标题来呈现一个带消息的确认对话框。
- confirmationDialog(_:item:titleVisibility:actions:message:) — 使用数据生成对话框内容,并使用文本视图作为消息来呈现一个带消息的确认对话框。
- dismissalConfirmationDialog(_:shouldPresent:actions:message:) — 当关闭动作已被触发时呈现一个确认对话框。
对话框配置
- dialogIcon(_:) — 配置该视图内对话框使用的图标。
- dialogSeverity(_:)
- dialogSuppressionToggle(isSuppressed:) — 允许用户抑制在
self中呈现的对话框和提醒,在 macOS 上使用默认的抑制消息。在其他平台上不使用。 - dialogSuppressionToggle(_:isSuppressed:) — 允许用户抑制在
self中呈现的对话框和提醒,在 macOS 上使用自定义的抑制消息。在其他平台上不使用。 - dialogPreventsAppTermination(_:) — 提醒或确认对话框是否阻止系统或“退出应用”菜单项退出/终止该应用。
工作表
- 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 时呈现一个浮层。
工作表与浮层配置
- interactiveDismissDisabled(_:) — 有条件地阻止浮层、工作表和检查器等呈现的交互式关闭。
- presentationDetents(_:) — 设置所属工作表可用的停靠高度。
- presentationDetents(_:selection:) — 设置所属工作表可用的停靠高度,并让你以编程方式控制当前选中的停靠高度。
- presentationDragIndicator(_:) — 设置工作表顶部拖拽指示器的可见性。
- presentationBackground(_:) — 使用形状样式设置所属工作表的呈现背景。
- presentationBackground(alignment:content:) — 把所属工作表的呈现背景设置为自定义视图。
- presentationBackgroundInteraction(_:) — 控制人们是否可以与呈现视图后面的视图交互。
- presentationCompactAdaptation(horizontal:vertical:) — 指定呈现如何适应水平和垂直的紧凑尺寸类别。
- presentationCompactAdaptation(_:) — 指定呈现如何适应紧凑尺寸类别。
- presentationContentInteraction(_:) — 配置呈现上的轻扫手势行为。
- presentationCornerRadius(_:) — 请求呈现具有特定的圆角半径。
- presentationSizing(_:) — 设置所属呈现的尺寸。
- presentationBreakthroughEffect(_:) — 改变所属呈现突破遮挡它的内容的方式。
- presentationPreventsAppTermination(_:) — 呈现是否阻止系统或“退出应用”菜单项终止/退出该应用。
- presentationPlacement(_:) — 设置呈现相对于呈现它的视图的位置。
- PresentationPlacement — 呈现相对于呈现它的视图的位置。
文件导出器
- 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配置文件名栏的标签。 - fileExporter(isPresented:document:contentType:defaultFilename:onCompletion:) — 呈现一个系统对话框,用于把存储在值类型(例如结构体)中的文档导出到磁盘上的文件。
- fileExporter(isPresented:documents:contentType:onCompletion:) — 呈现一个系统对话框,用于把一组值类型文档导出为磁盘上的文件。
- fileExporter(isPresented:document:contentTypes:defaultFilename:onCompletion:onCancellation:) — 呈现一个系统对话框,让用户把一个
FileDocument导出到磁盘上的文件。
文件导入器
- 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。
注视点流式传输
- foveatedStreamingPauseSheet(session:) — 告诉系统在注视点流式传输会话暂停时,呈现一个带有继续或结束该会话控件的工作表。
屏幕录制
- recordingEditor(_:) — 为给定的录制 URL 呈现录制编辑器。
- recordingEditor(_:mode:) — 以特定模式为给定的录制 URL 呈现录制编辑器。
文稿浏览器
- documentLaunchTitle(_:) — 设置显示在文稿启动卡片上的标题。
- documentLaunchSubtitle(_:) — 设置显示在文稿启动卡片标题下方的副标题。
- documentBrowserContextMenu(_:) — 为
DocumentLaunchView添加接受一组选中文件作为参数的动作。
检查器
- inspector(isPresented:content:) — 在视图层级的指定位置插入一个检查器。
- inspectorColumnWidth(_:) — 当以尾部列的形式呈现时,为包含该视图的检查器设置固定的首选宽度。
- inspectorColumnWidth(min:ideal:max:) — 为以尾部列形式呈现的检查器设置灵活的首选宽度。
快速查看预览
- quickLookPreview(_:) — 呈现单个 URL 内容的快速查看预览。
- quickLookPreview(_:in:) — 呈现你提供的各个 URL 的快速查看预览。
家人共享
- familyActivityPicker(isPresented:selection:) — 以工作表形式呈现活动选择器视图。
- familyActivityPicker(headerText:footerText:isPresented:selection:) — 以工作表形式呈现活动选择器视图。
- familyActivityPicker(title:headerText:footerText:isPresented:selection:) — 呈现一个活动选择器工作表,用于选择要管理的应用和网站。
实时活动
- activitySystemActionForegroundColor(_:) — 系统在锁定屏幕上实时活动旁边显示的辅助操作按钮的文本颜色。
- activityBackgroundTint(_:) — 设置出现在锁定屏幕上的实时活动背景的着色颜色。
游戏存档
- gameSaveSyncingAlert(directory:finishedLoading:) — 在游戏的同步目录载入期间呈现一个模态视图。
Apple Music
- musicSubscriptionOffer(isPresented:options:onLoadCompletion:) — 当
isPresented绑定为true时,开始呈现带有 Apple Music 订阅优惠的工作表。
联系人
- contactAccessButtonCaption(_:)
- contactAccessButtonStyle(_:)
- contactAccessPicker(isPresented:completionHandler:) — 以模态方式呈现界面,让用户选择你的应用可以访问哪些联系人。
StoreKit
- appStoreOverlay(isPresented:configuration:) — 当给定条件为 true 时呈现一个 StoreKit 浮层。
- appStoreMerchandising(isPresented:kind:onDismiss:) — 显示一个商品推广视图。
- manageSubscriptionsSheet(isPresented:)
- refundRequestSheet(for:isPresented:onDismiss:) — 为给定交易显示退款请求工作表。
- offerCodeRedemption(options:isPresented:onCompletion:) — 呈现一个工作表,让顾客可以兑换你在 App Store Connect 中配置的优惠码。
PhotoKit
- photosPicker(isPresented:selection:matching:preferredItemEncoding:) — 呈现一个选择
PhotosPickerItem的照片选择器。 - photosPicker(isPresented:selection:matching:preferredItemEncoding:photoLibrary:) — 呈现一个从给定照片图库中选择
PhotosPickerItem的照片选择器。 - photosPicker(isPresented:selection:maxSelectionCount:selectionBehavior:matching:preferredItemEncoding:) — 呈现一个选择一组
PhotosPickerItem的照片选择器。 - photosPicker(isPresented:selection:maxSelectionCount:selectionBehavior:matching:preferredItemEncoding:photoLibrary:) — 呈现一个从给定照片图库中选择一组
PhotosPickerItem的照片选择器。 - photosPickerAccessoryVisibility(_:edges:) — 设置照片选择器辅助元素的可见性。辅助元素包括内容与边缘之间的任何东西,例如导航栏或边栏。
- photosPickerDisabledCapabilities(_:) — 停用照片选择器的部分能力。
- photosPickerSearchText(_:) — 设置照片选择器的搜索文本。
- photosPickerStyle(_:) — 设置照片选择器的模式。
- photosSharedAlbumCreationSheet(isPresented:defaultTitle:defaultSharingPolicy:photoLibrary:onCompletion:) — 呈现一个让用户创建新的共享相簿的视图。
- photosSharedAlbumCustomizationSheet(isPresented:albumIdentifier:photoLibrary:onCompletion:) — 呈现一个让用户自定义指定共享相簿的视图。
- photosSharedAlbumPostingSheet(isPresented:items:defaultAlbumIdentifier:photoLibrary:completion:) — 呈现一个“添加到共享相簿”工作表,让用户把给定项目发布到共享相簿。
翻译
- translationPresentation(isPresented:text:attachmentAnchor:arrowEdge:replacementAction:) — 当给定条件为 true 时呈现一个翻译浮层。
- translationTask(_:action:) — 添加一个在该视图出现之前或翻译配置变化时执行的任务。
- translationTask(source:target:action:) — 添加一个在该视图出现之前或指定的源语言或目标语言变化时执行的任务。
- translationTask(source:target:preferredStrategy:action:) — 添加一个在该视图出现之前或指定的源语言或目标语言变化时执行的任务。
安全性
- certificateSheet(trust:title:message:help:) — 使用提供的证书信任显示一个证书工作表。