├── README.md ├── SwiftUI-PopToRootExample.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── xcuserdata │ └── Chuck.xcuserdatad │ └── xcschemes │ └── xcschememanagement.plist └── SwiftUI-PopToRootExample ├── AppDelegate.swift ├── Assets.xcassets ├── AppIcon.appiconset │ └── Contents.json └── Contents.json ├── Base.lproj └── LaunchScreen.storyboard ├── ContentView.swift ├── Info.plist ├── Preview Content └── Preview Assets.xcassets │ └── Contents.json ├── RootPresentationMode.swift ├── SceneDelegate.swift └── SwiftUI-PopToRootExample.entitlements /README.md: -------------------------------------------------------------------------------- 1 | # SwiftUI-PopToRootExample 2 | 3 | This sample program demonstrates how to programatically pop to a previous view and 4 | also pop to the root view of a NavigationView's stack. 5 | 6 | During the iOS 13 Beta's, Apple showed us how to programatically pop one level: 7 | https://stackoverflow.com/questions/56513568/ios-swiftui-pop-or-dismiss-view-programmatically/57279591#57279591 8 | 9 | However, Apple did not explicitly define how to "Pop to Root". So I asked this question on StackOverflow in August 2019: 10 | https://stackoverflow.com/questions/57334455/swiftui-how-to-pop-to-root-view 11 | 12 | The best answer to that question (from @malhal) pointed out the key point was that the NavigationLink's 13 | used in the stack must use .isDetailLink(false). This answer works, but instead of passing the bindings 14 | via view parameters, this can be simplified by using the environment (as pointed out by @Imthath). 15 | 16 | A key to understanding how this works is that when a user taps on a navigation link embedded in a navigation view, 17 | the 'isActive' property of this link is automatically set to true by SwiftUI. 18 | 19 | This project is an attempt to simplify a working solution to make it better. 20 | 21 | This project currently compiles and runs on Xcode Version 11.4.1 (11E503a) 22 | This project runs on iPhone, iPad and macOS. 23 | 24 | In addition to Apple's existing presentationMode @Environment key, 25 | the key component of this sample is the addition of the RootPresentationMode.swift file 26 | which defines a rootPresentationMode @Environment key and a RootPresentationMode type for it 27 | that use syntax and semantics similar to Apple's presentationMode. 28 | 29 | ======================= 30 | 31 | USAGE: 32 | 33 | 1. Add an .environment View Modifier to the root NavigationView to set the value of 34 | the \.rootPresentationMode key to a Binding of the Bool used to present the 35 | first child View. 36 | 37 | e.g. .environment(\.rootPresentationMode, self.$isActive) 38 | 39 | 2. Add an @Environment property wrapper to any view that will want to pop back to the 40 | root view. 41 | 42 | e.g. @Environment(\.rootPresentationMode) private var rootPresentationMode: Binding 43 | 44 | 3. Finally, invoking the dismiss() method on the wrapped value of the @Environment var 45 | will pop to the root View. 46 | 47 | e.g. self.rootPresentationMode.wrappedValue.dismiss() 48 | -------------------------------------------------------------------------------- /SwiftUI-PopToRootExample.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 136C30AF2475C14F00AFA412 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 136C30AE2475C14F00AFA412 /* AppDelegate.swift */; }; 11 | 136C30B12475C14F00AFA412 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 136C30B02475C14F00AFA412 /* SceneDelegate.swift */; }; 12 | 136C30B32475C14F00AFA412 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 136C30B22475C14F00AFA412 /* ContentView.swift */; }; 13 | 136C30B52475C15000AFA412 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 136C30B42475C15000AFA412 /* Assets.xcassets */; }; 14 | 136C30B82475C15000AFA412 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 136C30B72475C15000AFA412 /* Preview Assets.xcassets */; }; 15 | 136C30BB2475C15000AFA412 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 136C30B92475C15000AFA412 /* LaunchScreen.storyboard */; }; 16 | 136C30C32475C1E600AFA412 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 136C30C22475C1E600AFA412 /* README.md */; }; 17 | 136C30C62475C4AD00AFA412 /* RootPresentationMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 136C30C52475C4AD00AFA412 /* RootPresentationMode.swift */; }; 18 | /* End PBXBuildFile section */ 19 | 20 | /* Begin PBXFileReference section */ 21 | 136C30AB2475C14F00AFA412 /* SwiftUI-PopToRootExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "SwiftUI-PopToRootExample.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 22 | 136C30AE2475C14F00AFA412 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 23 | 136C30B02475C14F00AFA412 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; 24 | 136C30B22475C14F00AFA412 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; 25 | 136C30B42475C15000AFA412 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 26 | 136C30B72475C15000AFA412 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 27 | 136C30BA2475C15000AFA412 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 28 | 136C30BC2475C15000AFA412 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 29 | 136C30C22475C1E600AFA412 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 30 | 136C30C42475C36300AFA412 /* SwiftUI-PopToRootExample.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "SwiftUI-PopToRootExample.entitlements"; sourceTree = ""; }; 31 | 136C30C52475C4AD00AFA412 /* RootPresentationMode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RootPresentationMode.swift; sourceTree = ""; }; 32 | /* End PBXFileReference section */ 33 | 34 | /* Begin PBXFrameworksBuildPhase section */ 35 | 136C30A82475C14F00AFA412 /* Frameworks */ = { 36 | isa = PBXFrameworksBuildPhase; 37 | buildActionMask = 2147483647; 38 | files = ( 39 | ); 40 | runOnlyForDeploymentPostprocessing = 0; 41 | }; 42 | /* End PBXFrameworksBuildPhase section */ 43 | 44 | /* Begin PBXGroup section */ 45 | 136C30A22475C14F00AFA412 = { 46 | isa = PBXGroup; 47 | children = ( 48 | 136C30C22475C1E600AFA412 /* README.md */, 49 | 136C30AD2475C14F00AFA412 /* SwiftUI-PopToRootExample */, 50 | 136C30AC2475C14F00AFA412 /* Products */, 51 | ); 52 | sourceTree = ""; 53 | }; 54 | 136C30AC2475C14F00AFA412 /* Products */ = { 55 | isa = PBXGroup; 56 | children = ( 57 | 136C30AB2475C14F00AFA412 /* SwiftUI-PopToRootExample.app */, 58 | ); 59 | name = Products; 60 | sourceTree = ""; 61 | }; 62 | 136C30AD2475C14F00AFA412 /* SwiftUI-PopToRootExample */ = { 63 | isa = PBXGroup; 64 | children = ( 65 | 136C30AE2475C14F00AFA412 /* AppDelegate.swift */, 66 | 136C30B02475C14F00AFA412 /* SceneDelegate.swift */, 67 | 136C30B22475C14F00AFA412 /* ContentView.swift */, 68 | 136C30C52475C4AD00AFA412 /* RootPresentationMode.swift */, 69 | 136C30B42475C15000AFA412 /* Assets.xcassets */, 70 | 136C30B92475C15000AFA412 /* LaunchScreen.storyboard */, 71 | 136C30BC2475C15000AFA412 /* Info.plist */, 72 | 136C30C42475C36300AFA412 /* SwiftUI-PopToRootExample.entitlements */, 73 | 136C30B62475C15000AFA412 /* Preview Content */, 74 | ); 75 | path = "SwiftUI-PopToRootExample"; 76 | sourceTree = ""; 77 | }; 78 | 136C30B62475C15000AFA412 /* Preview Content */ = { 79 | isa = PBXGroup; 80 | children = ( 81 | 136C30B72475C15000AFA412 /* Preview Assets.xcassets */, 82 | ); 83 | path = "Preview Content"; 84 | sourceTree = ""; 85 | }; 86 | /* End PBXGroup section */ 87 | 88 | /* Begin PBXNativeTarget section */ 89 | 136C30AA2475C14F00AFA412 /* SwiftUI-PopToRootExample */ = { 90 | isa = PBXNativeTarget; 91 | buildConfigurationList = 136C30BF2475C15000AFA412 /* Build configuration list for PBXNativeTarget "SwiftUI-PopToRootExample" */; 92 | buildPhases = ( 93 | 136C30A72475C14F00AFA412 /* Sources */, 94 | 136C30A82475C14F00AFA412 /* Frameworks */, 95 | 136C30A92475C14F00AFA412 /* Resources */, 96 | ); 97 | buildRules = ( 98 | ); 99 | dependencies = ( 100 | ); 101 | name = "SwiftUI-PopToRootExample"; 102 | productName = "SwiftUI-PopToRootExample"; 103 | productReference = 136C30AB2475C14F00AFA412 /* SwiftUI-PopToRootExample.app */; 104 | productType = "com.apple.product-type.application"; 105 | }; 106 | /* End PBXNativeTarget section */ 107 | 108 | /* Begin PBXProject section */ 109 | 136C30A32475C14F00AFA412 /* Project object */ = { 110 | isa = PBXProject; 111 | attributes = { 112 | LastSwiftUpdateCheck = 1140; 113 | LastUpgradeCheck = 1250; 114 | ORGANIZATIONNAME = ForeTheGreen; 115 | TargetAttributes = { 116 | 136C30AA2475C14F00AFA412 = { 117 | CreatedOnToolsVersion = 11.4.1; 118 | }; 119 | }; 120 | }; 121 | buildConfigurationList = 136C30A62475C14F00AFA412 /* Build configuration list for PBXProject "SwiftUI-PopToRootExample" */; 122 | compatibilityVersion = "Xcode 9.3"; 123 | developmentRegion = en; 124 | hasScannedForEncodings = 0; 125 | knownRegions = ( 126 | en, 127 | Base, 128 | ); 129 | mainGroup = 136C30A22475C14F00AFA412; 130 | productRefGroup = 136C30AC2475C14F00AFA412 /* Products */; 131 | projectDirPath = ""; 132 | projectRoot = ""; 133 | targets = ( 134 | 136C30AA2475C14F00AFA412 /* SwiftUI-PopToRootExample */, 135 | ); 136 | }; 137 | /* End PBXProject section */ 138 | 139 | /* Begin PBXResourcesBuildPhase section */ 140 | 136C30A92475C14F00AFA412 /* Resources */ = { 141 | isa = PBXResourcesBuildPhase; 142 | buildActionMask = 2147483647; 143 | files = ( 144 | 136C30BB2475C15000AFA412 /* LaunchScreen.storyboard in Resources */, 145 | 136C30B82475C15000AFA412 /* Preview Assets.xcassets in Resources */, 146 | 136C30C32475C1E600AFA412 /* README.md in Resources */, 147 | 136C30B52475C15000AFA412 /* Assets.xcassets in Resources */, 148 | ); 149 | runOnlyForDeploymentPostprocessing = 0; 150 | }; 151 | /* End PBXResourcesBuildPhase section */ 152 | 153 | /* Begin PBXSourcesBuildPhase section */ 154 | 136C30A72475C14F00AFA412 /* Sources */ = { 155 | isa = PBXSourcesBuildPhase; 156 | buildActionMask = 2147483647; 157 | files = ( 158 | 136C30AF2475C14F00AFA412 /* AppDelegate.swift in Sources */, 159 | 136C30B12475C14F00AFA412 /* SceneDelegate.swift in Sources */, 160 | 136C30C62475C4AD00AFA412 /* RootPresentationMode.swift in Sources */, 161 | 136C30B32475C14F00AFA412 /* ContentView.swift in Sources */, 162 | ); 163 | runOnlyForDeploymentPostprocessing = 0; 164 | }; 165 | /* End PBXSourcesBuildPhase section */ 166 | 167 | /* Begin PBXVariantGroup section */ 168 | 136C30B92475C15000AFA412 /* LaunchScreen.storyboard */ = { 169 | isa = PBXVariantGroup; 170 | children = ( 171 | 136C30BA2475C15000AFA412 /* Base */, 172 | ); 173 | name = LaunchScreen.storyboard; 174 | sourceTree = ""; 175 | }; 176 | /* End PBXVariantGroup section */ 177 | 178 | /* Begin XCBuildConfiguration section */ 179 | 136C30BD2475C15000AFA412 /* Debug */ = { 180 | isa = XCBuildConfiguration; 181 | buildSettings = { 182 | ALWAYS_SEARCH_USER_PATHS = NO; 183 | CLANG_ANALYZER_NONNULL = YES; 184 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 185 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 186 | CLANG_CXX_LIBRARY = "libc++"; 187 | CLANG_ENABLE_MODULES = YES; 188 | CLANG_ENABLE_OBJC_ARC = YES; 189 | CLANG_ENABLE_OBJC_WEAK = YES; 190 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 191 | CLANG_WARN_BOOL_CONVERSION = YES; 192 | CLANG_WARN_COMMA = YES; 193 | CLANG_WARN_CONSTANT_CONVERSION = YES; 194 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 195 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 196 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 197 | CLANG_WARN_EMPTY_BODY = YES; 198 | CLANG_WARN_ENUM_CONVERSION = YES; 199 | CLANG_WARN_INFINITE_RECURSION = YES; 200 | CLANG_WARN_INT_CONVERSION = YES; 201 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 202 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 203 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 204 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 205 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 206 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 207 | CLANG_WARN_STRICT_PROTOTYPES = YES; 208 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 209 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 210 | CLANG_WARN_UNREACHABLE_CODE = YES; 211 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 212 | COPY_PHASE_STRIP = NO; 213 | DEBUG_INFORMATION_FORMAT = dwarf; 214 | ENABLE_STRICT_OBJC_MSGSEND = YES; 215 | ENABLE_TESTABILITY = YES; 216 | GCC_C_LANGUAGE_STANDARD = gnu11; 217 | GCC_DYNAMIC_NO_PIC = NO; 218 | GCC_NO_COMMON_BLOCKS = YES; 219 | GCC_OPTIMIZATION_LEVEL = 0; 220 | GCC_PREPROCESSOR_DEFINITIONS = ( 221 | "DEBUG=1", 222 | "$(inherited)", 223 | ); 224 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 225 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 226 | GCC_WARN_UNDECLARED_SELECTOR = YES; 227 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 228 | GCC_WARN_UNUSED_FUNCTION = YES; 229 | GCC_WARN_UNUSED_VARIABLE = YES; 230 | IPHONEOS_DEPLOYMENT_TARGET = 15.0; 231 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 232 | MTL_FAST_MATH = YES; 233 | ONLY_ACTIVE_ARCH = YES; 234 | SDKROOT = iphoneos; 235 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 236 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 237 | }; 238 | name = Debug; 239 | }; 240 | 136C30BE2475C15000AFA412 /* Release */ = { 241 | isa = XCBuildConfiguration; 242 | buildSettings = { 243 | ALWAYS_SEARCH_USER_PATHS = NO; 244 | CLANG_ANALYZER_NONNULL = YES; 245 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 246 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 247 | CLANG_CXX_LIBRARY = "libc++"; 248 | CLANG_ENABLE_MODULES = YES; 249 | CLANG_ENABLE_OBJC_ARC = YES; 250 | CLANG_ENABLE_OBJC_WEAK = YES; 251 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 252 | CLANG_WARN_BOOL_CONVERSION = YES; 253 | CLANG_WARN_COMMA = YES; 254 | CLANG_WARN_CONSTANT_CONVERSION = YES; 255 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 256 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 257 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 258 | CLANG_WARN_EMPTY_BODY = YES; 259 | CLANG_WARN_ENUM_CONVERSION = YES; 260 | CLANG_WARN_INFINITE_RECURSION = YES; 261 | CLANG_WARN_INT_CONVERSION = YES; 262 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 263 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 264 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 265 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 266 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 267 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 268 | CLANG_WARN_STRICT_PROTOTYPES = YES; 269 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 270 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 271 | CLANG_WARN_UNREACHABLE_CODE = YES; 272 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 273 | COPY_PHASE_STRIP = NO; 274 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 275 | ENABLE_NS_ASSERTIONS = NO; 276 | ENABLE_STRICT_OBJC_MSGSEND = YES; 277 | GCC_C_LANGUAGE_STANDARD = gnu11; 278 | GCC_NO_COMMON_BLOCKS = YES; 279 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 280 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 281 | GCC_WARN_UNDECLARED_SELECTOR = YES; 282 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 283 | GCC_WARN_UNUSED_FUNCTION = YES; 284 | GCC_WARN_UNUSED_VARIABLE = YES; 285 | IPHONEOS_DEPLOYMENT_TARGET = 15.0; 286 | MTL_ENABLE_DEBUG_INFO = NO; 287 | MTL_FAST_MATH = YES; 288 | SDKROOT = iphoneos; 289 | SWIFT_COMPILATION_MODE = wholemodule; 290 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 291 | VALIDATE_PRODUCT = YES; 292 | }; 293 | name = Release; 294 | }; 295 | 136C30C02475C15000AFA412 /* Debug */ = { 296 | isa = XCBuildConfiguration; 297 | buildSettings = { 298 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 299 | CODE_SIGN_ENTITLEMENTS = "SwiftUI-PopToRootExample/SwiftUI-PopToRootExample.entitlements"; 300 | CODE_SIGN_STYLE = Automatic; 301 | DEVELOPMENT_ASSET_PATHS = "\"SwiftUI-PopToRootExample/Preview Content\""; 302 | DEVELOPMENT_TEAM = 64WN437W3Z; 303 | ENABLE_PREVIEWS = YES; 304 | INFOPLIST_FILE = "SwiftUI-PopToRootExample/Info.plist"; 305 | LD_RUNPATH_SEARCH_PATHS = ( 306 | "$(inherited)", 307 | "@executable_path/Frameworks", 308 | ); 309 | PRODUCT_BUNDLE_IDENTIFIER = "com.ForeTheGreen.SwiftUI-PopToRootExample"; 310 | PRODUCT_NAME = "$(TARGET_NAME)"; 311 | SUPPORTS_MACCATALYST = YES; 312 | SWIFT_VERSION = 5.0; 313 | TARGETED_DEVICE_FAMILY = "1,2"; 314 | }; 315 | name = Debug; 316 | }; 317 | 136C30C12475C15000AFA412 /* Release */ = { 318 | isa = XCBuildConfiguration; 319 | buildSettings = { 320 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 321 | CODE_SIGN_ENTITLEMENTS = "SwiftUI-PopToRootExample/SwiftUI-PopToRootExample.entitlements"; 322 | CODE_SIGN_STYLE = Automatic; 323 | DEVELOPMENT_ASSET_PATHS = "\"SwiftUI-PopToRootExample/Preview Content\""; 324 | DEVELOPMENT_TEAM = 64WN437W3Z; 325 | ENABLE_PREVIEWS = YES; 326 | INFOPLIST_FILE = "SwiftUI-PopToRootExample/Info.plist"; 327 | LD_RUNPATH_SEARCH_PATHS = ( 328 | "$(inherited)", 329 | "@executable_path/Frameworks", 330 | ); 331 | PRODUCT_BUNDLE_IDENTIFIER = "com.ForeTheGreen.SwiftUI-PopToRootExample"; 332 | PRODUCT_NAME = "$(TARGET_NAME)"; 333 | SUPPORTS_MACCATALYST = YES; 334 | SWIFT_VERSION = 5.0; 335 | TARGETED_DEVICE_FAMILY = "1,2"; 336 | }; 337 | name = Release; 338 | }; 339 | /* End XCBuildConfiguration section */ 340 | 341 | /* Begin XCConfigurationList section */ 342 | 136C30A62475C14F00AFA412 /* Build configuration list for PBXProject "SwiftUI-PopToRootExample" */ = { 343 | isa = XCConfigurationList; 344 | buildConfigurations = ( 345 | 136C30BD2475C15000AFA412 /* Debug */, 346 | 136C30BE2475C15000AFA412 /* Release */, 347 | ); 348 | defaultConfigurationIsVisible = 0; 349 | defaultConfigurationName = Release; 350 | }; 351 | 136C30BF2475C15000AFA412 /* Build configuration list for PBXNativeTarget "SwiftUI-PopToRootExample" */ = { 352 | isa = XCConfigurationList; 353 | buildConfigurations = ( 354 | 136C30C02475C15000AFA412 /* Debug */, 355 | 136C30C12475C15000AFA412 /* Release */, 356 | ); 357 | defaultConfigurationIsVisible = 0; 358 | defaultConfigurationName = Release; 359 | }; 360 | /* End XCConfigurationList section */ 361 | }; 362 | rootObject = 136C30A32475C14F00AFA412 /* Project object */; 363 | } 364 | -------------------------------------------------------------------------------- /SwiftUI-PopToRootExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SwiftUI-PopToRootExample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /SwiftUI-PopToRootExample.xcodeproj/xcuserdata/Chuck.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | SwiftUI-PopToRootExample.xcscheme_^#shared#^_ 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /SwiftUI-PopToRootExample/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // SwiftUI-PopToRootExample 4 | // 5 | // Created by Chuck Hartman on 5/20/20. 6 | // Copyright © 2020 ForeTheGreen. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | 15 | 16 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 17 | // Override point for customization after application launch. 18 | return true 19 | } 20 | 21 | // MARK: UISceneSession Lifecycle 22 | 23 | func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { 24 | // Called when a new scene session is being created. 25 | // Use this method to select a configuration to create the new scene with. 26 | return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) 27 | } 28 | 29 | func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set) { 30 | // Called when the user discards a scene session. 31 | // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. 32 | // Use this method to release any resources that were specific to the discarded scenes, as they will not return. 33 | } 34 | 35 | 36 | } 37 | 38 | -------------------------------------------------------------------------------- /SwiftUI-PopToRootExample/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "scale" : "2x", 6 | "size" : "20x20" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "scale" : "3x", 11 | "size" : "20x20" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "scale" : "2x", 16 | "size" : "29x29" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "scale" : "3x", 21 | "size" : "29x29" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "scale" : "2x", 26 | "size" : "40x40" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "scale" : "3x", 31 | "size" : "40x40" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "scale" : "2x", 36 | "size" : "60x60" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "scale" : "3x", 41 | "size" : "60x60" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "scale" : "1x", 46 | "size" : "20x20" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "scale" : "2x", 51 | "size" : "20x20" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "scale" : "1x", 56 | "size" : "29x29" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "scale" : "2x", 61 | "size" : "29x29" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "scale" : "1x", 66 | "size" : "40x40" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "scale" : "2x", 71 | "size" : "40x40" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "scale" : "1x", 76 | "size" : "76x76" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "scale" : "2x", 81 | "size" : "76x76" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "scale" : "2x", 86 | "size" : "83.5x83.5" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "scale" : "1x", 91 | "size" : "1024x1024" 92 | } 93 | ], 94 | "info" : { 95 | "author" : "xcode", 96 | "version" : 1 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /SwiftUI-PopToRootExample/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /SwiftUI-PopToRootExample/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /SwiftUI-PopToRootExample/ContentView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ContentView.swift 3 | // SwiftUI-PopToRootExample 4 | // 5 | // Created by Chuck Hartman on 5/20/20. 6 | // Copyright © 2020 ForeTheGreen. All rights reserved. 7 | // 8 | 9 | import SwiftUI 10 | 11 | struct ContentView: View { 12 | @State private var isActive : Bool = false 13 | var body: some View { 14 | NavigationView { 15 | VStack { 16 | Text("Root") 17 | NavigationLink(destination: ContentView2(), isActive: self.$isActive ) 18 | { Text("Push") } 19 | .isDetailLink(false) 20 | } 21 | .navigationBarTitle("Root") 22 | } 23 | .navigationViewStyle(StackNavigationViewStyle()) 24 | .environment(\.rootPresentationMode, self.$isActive) 25 | } 26 | } 27 | 28 | struct ContentView2: View { 29 | @State private var isActive : Bool = false 30 | @Environment(\.presentationMode) private var presentationMode: Binding 31 | @Environment(\.rootPresentationMode) private var rootPresentationMode: Binding 32 | var body: some View { 33 | VStack { 34 | Text("Two") 35 | NavigationLink(destination: ContentView3(), isActive: self.$isActive) 36 | { Text("Push") } 37 | .isDetailLink(false) 38 | Button (action: { self.presentationMode.wrappedValue.dismiss() } ) 39 | { Text("Pop") } 40 | Button (action: { self.rootPresentationMode.wrappedValue.dismiss() } ) 41 | { Text("Pop to root") } 42 | } 43 | .navigationBarTitle("Two") 44 | } 45 | } 46 | struct ContentView3: View { 47 | @State private var isActive : Bool = false 48 | @Environment(\.presentationMode) private var presentationMode: Binding 49 | @Environment(\.rootPresentationMode) private var rootPresentationMode: Binding 50 | var body: some View { 51 | VStack { 52 | Text("Three") 53 | NavigationLink(destination: ContentView4(), isActive: self.$isActive) 54 | { Text("Push") } 55 | .isDetailLink(false) 56 | Button (action: { self.presentationMode.wrappedValue.dismiss() } ) 57 | { Text("Pop") } 58 | Button (action: { self.rootPresentationMode.wrappedValue.dismiss() } ) 59 | { Text("Pop to root") } 60 | } 61 | .navigationBarTitle("Three") 62 | } 63 | } 64 | 65 | struct ContentView4: View { 66 | @Environment(\.presentationMode) private var presentationMode: Binding 67 | @Environment(\.rootPresentationMode) private var rootPresentationMode: Binding 68 | var body: some View { 69 | VStack { 70 | Text("Four") 71 | Button (action: { self.presentationMode.wrappedValue.dismiss() } ) 72 | { Text("Pop") } 73 | Button (action: { self.rootPresentationMode.wrappedValue.dismiss() } ) 74 | { Text("Pop to root") } 75 | } 76 | .navigationBarTitle("Four") 77 | } 78 | } 79 | 80 | struct ContentView_Previews: PreviewProvider { 81 | static var previews: some View { 82 | ContentView() 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /SwiftUI-PopToRootExample/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | $(PRODUCT_BUNDLE_PACKAGE_TYPE) 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UIApplicationSceneManifest 24 | 25 | UIApplicationSupportsMultipleScenes 26 | 27 | UISceneConfigurations 28 | 29 | UIWindowSceneSessionRoleApplication 30 | 31 | 32 | UISceneConfigurationName 33 | Default Configuration 34 | UISceneDelegateClassName 35 | $(PRODUCT_MODULE_NAME).SceneDelegate 36 | 37 | 38 | 39 | 40 | UILaunchStoryboardName 41 | LaunchScreen 42 | UIRequiredDeviceCapabilities 43 | 44 | armv7 45 | 46 | UISupportedInterfaceOrientations 47 | 48 | UIInterfaceOrientationPortrait 49 | UIInterfaceOrientationLandscapeLeft 50 | UIInterfaceOrientationLandscapeRight 51 | 52 | UISupportedInterfaceOrientations~ipad 53 | 54 | UIInterfaceOrientationPortrait 55 | UIInterfaceOrientationPortraitUpsideDown 56 | UIInterfaceOrientationLandscapeLeft 57 | UIInterfaceOrientationLandscapeRight 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /SwiftUI-PopToRootExample/Preview Content/Preview Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /SwiftUI-PopToRootExample/RootPresentationMode.swift: -------------------------------------------------------------------------------- 1 | // 2 | // RootPresentationMode.swift 3 | // SwiftUI-PopToRootExample 4 | // 5 | // Created by Chuck Hartman on 5/20/20. 6 | // Copyright © 2020 ForeTheGreen. All rights reserved. 7 | // 8 | 9 | import SwiftUI 10 | 11 | struct RootPresentationModeKey: EnvironmentKey { 12 | static let defaultValue: Binding = .constant(RootPresentationMode()) 13 | } 14 | 15 | extension EnvironmentValues { 16 | var rootPresentationMode: Binding { 17 | get { return self[RootPresentationModeKey.self] } 18 | set { self[RootPresentationModeKey.self] = newValue } 19 | } 20 | } 21 | 22 | typealias RootPresentationMode = Bool 23 | 24 | extension RootPresentationMode { 25 | 26 | public mutating func dismiss() { 27 | self.toggle() 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /SwiftUI-PopToRootExample/SceneDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SceneDelegate.swift 3 | // SwiftUI-PopToRootExample 4 | // 5 | // Created by Chuck Hartman on 5/20/20. 6 | // Copyright © 2020 ForeTheGreen. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import SwiftUI 11 | 12 | class SceneDelegate: UIResponder, UIWindowSceneDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { 18 | // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. 19 | // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. 20 | // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). 21 | 22 | // Create the SwiftUI view that provides the window contents. 23 | let contentView = ContentView() 24 | 25 | // Use a UIHostingController as window root view controller. 26 | if let windowScene = scene as? UIWindowScene { 27 | let window = UIWindow(windowScene: windowScene) 28 | window.rootViewController = UIHostingController(rootView: contentView) 29 | self.window = window 30 | window.makeKeyAndVisible() 31 | } 32 | } 33 | 34 | func sceneDidDisconnect(_ scene: UIScene) { 35 | // Called as the scene is being released by the system. 36 | // This occurs shortly after the scene enters the background, or when its session is discarded. 37 | // Release any resources associated with this scene that can be re-created the next time the scene connects. 38 | // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead). 39 | } 40 | 41 | func sceneDidBecomeActive(_ scene: UIScene) { 42 | // Called when the scene has moved from an inactive state to an active state. 43 | // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive. 44 | } 45 | 46 | func sceneWillResignActive(_ scene: UIScene) { 47 | // Called when the scene will move from an active state to an inactive state. 48 | // This may occur due to temporary interruptions (ex. an incoming phone call). 49 | } 50 | 51 | func sceneWillEnterForeground(_ scene: UIScene) { 52 | // Called as the scene transitions from the background to the foreground. 53 | // Use this method to undo the changes made on entering the background. 54 | } 55 | 56 | func sceneDidEnterBackground(_ scene: UIScene) { 57 | // Called as the scene transitions from the foreground to the background. 58 | // Use this method to save data, release shared resources, and store enough scene-specific state information 59 | // to restore the scene back to its current state. 60 | } 61 | 62 | 63 | } 64 | 65 | -------------------------------------------------------------------------------- /SwiftUI-PopToRootExample/SwiftUI-PopToRootExample.entitlements: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | com.apple.security.app-sandbox 6 | 7 | com.apple.security.network.client 8 | 9 | 10 | 11 | --------------------------------------------------------------------------------