Created
January 25, 2024 10:27
-
-
Save Codelaby/55fdd06dc4c47eea1bfd3bdf54ccb03c to your computer and use it in GitHub Desktop.
Inspector SwiftUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
//https://dimillian.medium.com/how-to-use-the-new-inspector-swiftui-view-modifier-9cefb8353beb | |
struct InspectorPanel: View { | |
@State var isShowingInspector = false | |
var body: some View { | |
NavigationStack { | |
VStack { | |
Button("Press open second panel") { | |
isShowingInspector.toggle() | |
} | |
.inspector(isPresented: $isShowingInspector) { | |
if UIDevice.current.userInterfaceIdiom == .phone { | |
SecondPanelView(isPresented: $isShowingInspector) | |
//.background(.green) | |
} else { | |
SecondPanelView(isPresented: $isShowingInspector) | |
//.background(.red) | |
} | |
//.inspectorColumnWidth(min: 50, ideal: 240, max: 320) | |
} | |
} | |
//.frame(maxWidth: .infinity, maxHeight: .infinity) | |
.border(.red) | |
.navigationBarTitle("First View", displayMode: .inline) | |
} | |
} | |
} | |
struct SecondPanelView: View { | |
@Environment(\.verticalSizeClass) var verticalSizeClass: UserInterfaceSizeClass? | |
@Environment(\.horizontalSizeClass) var horizontalSizeClass: UserInterfaceSizeClass? | |
@Binding var isPresented: Bool | |
var body: some View { | |
NavigationStack { | |
ScrollView(.vertical, showsIndicators: true) { | |
VStack() { | |
let _ = print("horizontal \(String(describing: horizontalSizeClass))") | |
let _ = print("vertical \(String(describing: verticalSizeClass))") | |
if horizontalSizeClass == .compact && verticalSizeClass == .regular { | |
Text("iPhone Portrait") | |
} | |
else if horizontalSizeClass == .compact && verticalSizeClass == .compact { | |
Text("iPhone Landscape") | |
Button(action: { | |
isPresented.toggle() | |
},label: { | |
Image(systemName: "sidebar.right") | |
.foregroundColor(.secondary) | |
}) | |
} | |
else if horizontalSizeClass == .regular && verticalSizeClass == .regular { | |
Text("iPad Portrait/Landscape") | |
} | |
Text(generateParagraphs(3)) | |
.padding() | |
} | |
} | |
.frame(maxWidth: .infinity, maxHeight: .infinity) | |
.toolbar { | |
if UIDevice.current.userInterfaceIdiom == .pad { | |
cancelToolbarButton | |
} | |
} | |
.border(.green) | |
} | |
} | |
@ToolbarContentBuilder | |
private var cancelToolbarButton: some ToolbarContent { | |
ToolbarItem(placement: .navigationBarTrailing) { | |
Button(action: { | |
isPresented.toggle() | |
},label: { | |
Image(systemName: "sidebar.right") | |
.foregroundColor(.secondary) | |
}) | |
} | |
} | |
} | |
#Preview { | |
InspectorPanel() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment