Skip to content

Instantly share code, notes, and snippets.

View gahntpo's full-sized avatar

Karin Prater gahntpo

View GitHub Profile
@gahntpo
gahntpo / NavigationTestView.swift
Last active June 20, 2020 15:45
keeping a state var for a navigationLink
import SwiftUI
struct NavigationTestView: View {
@State var detailIsShown: Bool = false
var body: some View {
NavigationView {
VStack {
Text("Main view")
.font(.title)
@gahntpo
gahntpo / ContentView.swift
Created June 20, 2020 15:33
default contentView created for tabbed app
import SwiftUI
struct ContentView: View {
@State private var selection = 0
var body: some View {
TabView(selection: $selection){
Text("First View")
.font(.title)
.tabItem {
@gahntpo
gahntpo / Main2View
Created June 20, 2020 09:56
NavigationLink outside conditional view layout
struct MainView: View {
@EnvironmentObject var windowManager: WindowSizeManager
@State var detailIsShown: Bool = false
var body: some View {
NavigationView {
ConditionalStack(isHorizonalStack: windowManager.isLandscape) {
Text("main view")
@gahntpo
gahntpo / MainView.swift
Created June 20, 2020 09:53
conditional layout and NavigationView
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)
@gahntpo
gahntpo / FlowStack.swift
Last active June 20, 2020 09:34
stacking views horizontal or vertical
import SwiftUI
struct FlowStack<Content>: View where Content: View {
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}
var content: () -> Content
@EnvironmentObject var windowManager: WindowSizeManager
@gahntpo
gahntpo / WindowSize2Manager.swift
Created June 20, 2020 09:28
using Combine in your ObservableObject
import Combine
import SwiftUI
class WindowSizeManager: ObservableObject {
@Published var isLandscape: Bool = false
@Published var windowSceneBounds: CGRect = CGRect.zero
@Published var isSimilarToPhone: Bool = false
@gahntpo
gahntpo / AdaptiveStackExamples.swift
Created June 20, 2020 09:15
using VStack or HStack depending on device orientation
import SwiftUI
struct AdaptiveStackExamples: View {
@EnvironmentObject var windowManager: WindowSizeManager
var body: some View {
VStack(spacing: 20) {
VStack {
@gahntpo
gahntpo / ConditionalStack.swift
Created June 20, 2020 09:12
ViewBuilder to conditionally use a HStack or VStack
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) {
@gahntpo
gahntpo / AdjustedView_Previews.swift
Last active June 20, 2020 09:10
using the WindowSizeManager with the PreviewProvider in SwiftUI
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")
@gahntpo
gahntpo / WindowSizeManager.swift
Created June 20, 2020 09:03
adding a helper var to the class to create an instance with the right window dimensions
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)