在SwiftUI中相当于UISplitViewController [英] What's the equivalent of the UISplitViewController in SwiftUI

查看:37
本文介绍了在SwiftUI中相当于UISplitViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现一个与iPad和iPhone中的默认Mail应用程序接近的UI.

I need to implement an UI which close to the default Mail app in iPad and iPhone.

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

The App has two sections, typically, the master view will be displayed on the left side and detail view will be displayed in the right side in iPad.

在电话中,主视图将显示在整个屏幕上,详细视图可以按在第二屏幕上.

In the phone, the master view will be displayed on whole screen, the detail view can be pushed as second screen.

如何在新的SwiftUI中实现它

How to implement it in the new SwiftUI

推荐答案

在SwiftUI中确实没有SplitView,但是当您使用以下代码时,您描述的内容将自动完成:

There is not really a SplitView in SwiftUI, but what you describe is automatically accomplished when you use the following code:

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本身的原因.

This is also why the .navigationBarTitle() modifier should be applied on the view inside the NavigationView, instead of on the NavigationView itself.

奖金:如果您喜欢splitView navigationViewStyle,则可以在NavigationView上应用 .navigationViewStyle(StackNavigationViewStyle())修饰符.

Bonus: if you don't like the splitView navigationViewStyle, you can apply the .navigationViewStyle(StackNavigationViewStyle()) modifier on the NavigationView.

我发现NavigationLink具有一个 isDetailLink(Bool)修饰符.默认值显示为 true ,因为默认情况下,目标"显示在详细信息视图(右侧)中.但是,当您将此修饰符设置为 false 时,目标将作为主文件显示(在左侧).

I discovered that the NavigationLink has an isDetailLink(Bool) modifier. The default value appears to be true, because by default the "destination" is presented in the detail view (on the right). But when you set this modifier to false, the destination is presented as a master (on the left).

这篇关于在SwiftUI中相当于UISplitViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆