The macOS code shown below uses SwiftUI to create a two column NavigationSplitView. The detail views are different sizes but the window does not change size when the detail view changes. How can I adjust the window size based on the detail view?
import SwiftUI
struct AppleView: View {
var body: some View {
Text("Apple View 🍎").font(.title)
.frame(width: 500, height: 300)
.background(.purple)
}
}
struct KiwiView: View {
var body: some View {
Text("Kiwi View 🥝").font(.title)
.frame(width: 400, height: 500)
.background(.green)
}
}
enum Fruit: String, CaseIterable {
case apple = "Apple"
case kiwi = "Kiwi"
}
struct DetailView: View {
@Binding var selectedItem: Fruit
var body: some View {
switch selectedItem {
case .apple:
AppleView()
case .kiwi:
KiwiView()
}
}
}
struct ContentView: View {
@State private var selectedFruit: Fruit = .apple
var body: some View {
NavigationSplitView {
List(Fruit.allCases, id: \.self, selection: $selectedFruit) { fruit in
Text(fruit.rawValue)
}
} detail: {
DetailView(selectedItem: $selectedFruit)
}
}
}