SwiftUI 中 UISplitViewController 的等价物是什么 [英] What's the equivalent of the UISplitViewController in SwiftUI
问题描述
我需要实现一个与 iPad 和 iPhone 中的默认邮件应用程序接近的 UI.
I need to implement an UI which close to the default Mail app in iPad and iPhone.
App 有两个部分,通常情况下,在 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
You can display a list for example")
}
}
.navigationBarTitle("Master")
// The second View is the Detail
Text("Detail placeholder
Here 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()
修饰符应该应用于 inside 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
时,目标将显示为 master(在左侧).
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屋!