我正在尝试创建可在iOS和iPadOS上运行的Master / Detail应用。当我将应用旋转到横向时,它可以在iPadOS上按预期运行;但是,在iOS上,主视图会覆盖“详细信息”视图,而不是缩小“详细信息”以适合拆分屏幕。我可能在其中一个“视图”上缺少修改器,但是找不到解决方案。任何帮助将不胜感激。
这里是代码:
struct ContentView: View {
var body: some View {
VStack(alignment: .leading) {
NavigationView {
MasterView().navigationBarTitle("Star Wars Characters")
DetailView()
}.navigationViewStyle(DoubleColumnNavigationViewStyle())
}
}
}
struct MasterView: View {
@ObservedObject var dataStore = DataStore()
var body: some View {
List {
ForEach(sortedPeople) { person in
NavigationLink(destination: DetailView(person: person)) {
PersonView(person: person)
}
}
}
}
var sortedPeople: [Person] {
return dataStore.people.sorted {$0.firstName < $1.firstName}
}
}
struct PersonView: View {
var person: Person
var body: some View {
VStack(alignment: .leading){
Text("\(person.lastName)").font(.headline).fontWeight(.ultraLight)
Text("\(person.firstName)").font(.subheadline).fontWeight(.light)
}
.padding(.leading)
}
}
struct DetailView: View {
var person = Person()
var body: some View {
VStack(alignment: .leading, spacing: 5) {
Text(displayText()).font(.title)
}.padding()
.navigationBarTitle("👋🏻")
}
func displayText() -> String {
return person.firstName != "" ? "Hello \(person.name)" : "Click on someone to say hi"
}
}
这是输出:
如所指出,我理解这是默认行为。但是,如果您查看Apple的Contact应用程序,似乎可以实现desired行为。