This file contains hidden or 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 | |
| struct ContentView: View { | |
| @State private var selection = 0 | |
| var body: some View { | |
| TabView(selection: $selection){ | |
| Text("First View") | |
| .font(.title) | |
| .tabItem { |
This file contains hidden or 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
| struct MainView: View { | |
| @EnvironmentObject var windowManager: WindowSizeManager | |
| @State var detailIsShown: Bool = false | |
| var body: some View { | |
| NavigationView { | |
| ConditionalStack(isHorizonalStack: windowManager.isLandscape) { | |
| Text("main view") |
This file contains hidden or 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 | |
| struct MainView: View { | |
| @EnvironmentObject var windowManager: WindowSizeManager | |
| var body: some View { | |
| NavigationView { | |
| ConditionalStack(isHorizonalStack: windowManager.isLandscape) { | |
| Text("This is the main view").font(.title) |
This file contains hidden or 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 | |
| struct FlowStack<Content>: View where Content: View { | |
| init(@ViewBuilder content: @escaping () -> Content) { | |
| self.content = content | |
| } | |
| var content: () -> Content | |
| @EnvironmentObject var windowManager: WindowSizeManager |
This file contains hidden or 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 Combine | |
| import SwiftUI | |
| class WindowSizeManager: ObservableObject { | |
| @Published var isLandscape: Bool = false | |
| @Published var windowSceneBounds: CGRect = CGRect.zero | |
| @Published var isSimilarToPhone: Bool = false |
This file contains hidden or 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 | |
| struct AdaptiveStackExamples: View { | |
| @EnvironmentObject var windowManager: WindowSizeManager | |
| var body: some View { | |
| VStack(spacing: 20) { | |
| VStack { |
This file contains hidden or 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 | |
| struct ConditionalStack<Content>: View where Content: View { | |
| init(alignment: HorizontalAlignment = .center, | |
| verticalAlignment: VerticalAlignment = .center, | |
| spacing: CGFloat = 10, | |
| verticalSpacing: CGFloat = 10, | |
| isHorizonalStack: Bool, | |
| @ViewBuilder content: @escaping () -> Content) { |
This file contains hidden or 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
| struct AdaptByHidingView_Previews: PreviewProvider { | |
| static var previews: some View { | |
| let windowLandscapeManager = WindowSizeManager.defaultFor(deviceType: .phone11, | |
| isLandscape: true) | |
| return Group { | |
| AdaptByHidingView() | |
| .environmentObject(WindowSizeManager.defaultFor(deviceType: .phone5s, | |
| isLandscape: false)) | |
| .previewDevice(PreviewDevice(rawValue: "iPhone SE (2nd generation)")) | |
| .previewDisplayName("iPhone SE") |
This file contains hidden or 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
| static func defaultFor(deviceType: DeviceType, isLandscape: Bool = false) -> WindowSizeManager { | |
| let manager = WindowSizeManager() | |
| manager.isLandscape = isLandscape | |
| switch deviceType { | |
| case .mini: | |
| manager.windowSceneBounds = isLandscape ? CGRect(x: 0, y: 0, width: 1024, height: 768) : CGRect(x: 0, y: 0, width: 768, height: 1024) | |
| case .eleven: | |
| manager.windowSceneBounds = isLandscape ? CGRect(x: 0, y: 0, width: 1194, height: 834) : CGRect(x: 0, y: 0, width: 834, height: 1194) | |
| case .twelve: | |
| manager.windowSceneBounds = isLandscape ? CGRect(x: 0, y: 0, width: 1366, height: 1024) : CGRect(x: 0, y: 0, width: 1024, height: 1366) |