ios - SwiftUI 中的 UISplitViewController 等价物是什么
问题描述
我需要实现一个接近 iPad 和 iPhone 中默认邮件应用程序的 UI。
该应用程序有两个部分,通常,主视图将显示在左侧,详细视图将显示在 iPad 的右侧。
在手机中,主视图将显示在整个屏幕上,详细视图可以作为第二屏推送。
如何在新的 SwiftUI 中实现它
解决方案
SwiftUI 中并没有真正的 SplitView,但是您描述的内容在您使用以下代码时会自动完成:
import SwiftUI
struct MyView: View {
var body: some View {
NavigationView {
// The first View inside the NavigationView is the Master
List(1 ... 5, id: \.self) { x in
NavigationLink(destination: SecondView(detail: x)) {
Text("Master\nYou can display a list for example")
}
}
.navigationBarTitle("Master")
// The second View is the Detail
Text("Detail placeholder\nHere you could display something when nothing from the list is selected")
.navigationBarTitle("Detail")
}
}
}
struct SecondView: View {
var detail: Int
var body: some View {
Text("Now you are seeing the real detail View! This is detail \(detail)")
}
}
这也是为什么.navigationBarTitle()
修饰符应该应用于NavigationView内部的视图,而不是 NavigationView 本身的原因。
奖励:如果您不喜欢 splitView navigationViewStyle,您可以.navigationViewStyle(StackNavigationViewStyle())
在 NavigationView 上应用修饰符。
编辑:我发现 NavigationLink 有一个isDetailLink(Bool)
修饰符。默认值似乎是true
,因为默认情况下“目标”显示在详细视图(右侧)中。但是,当您将此修饰符设置为 时false
,目标将显示为主节点(在左侧)。
推荐阅读
- java - springdoc-openapi 通用控制器类型参数支持
- sql-server - SQL Server - 如何从不与另一个字段值重复的字段组合中强制执行字段值
- php - MYSQLI多查询功能
- javascript - 调用 Playkit-js 播放器未在浏览器中显示
- c# - System.InvalidOperationException: '没有 'IEnumerable 类型的 ViewData 项
'这有关键的'项目'。 - python - 删除2个标签python之间的文本
- azure-ad-b2c - 使用 Azure B2C 和 MSAL.NET 获取多个 WebAPI 访问令牌的 AcquireTokenSilent
- android - 它必须签署apk的原因是什么?
- angular - 键入 @ 时 Angular 中的自动建议下拉菜单
- android - 亚马逊设备的 Admob 问题