首页 > 解决方案 > SwiftUI 中的 UISplitViewController 等价物是什么

问题描述

我需要实现一个接近 iPad 和 iPhone 中默认邮件应用程序的 UI。

该应用程序有两个部分,通常,主视图将显示在左侧,详细视图将显示在 iPad 的右侧。

在手机中,主视图将显示在整个屏幕上,详细视图可以作为第二屏推送。

如何在新的 SwiftUI 中实现它

标签: iosswiftswiftui

解决方案


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,目标将显示为主节点(在左侧)。


推荐阅读