View+dismissTop.swift (3647B)
1 // MIT License 2 // Copyright © Nicolai Harbo 3 // 4 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software 5 // and associated documentation files (the "Software"), to deal in the Software without restriction, 6 // including without limitation the rights to use, copy, modify, merge, publish, distribute, 7 // sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in all copies or 11 // substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 14 // BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 16 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 17 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 18 // 19 /** 20 * @author Marc Stibane 21 */ 22 import SwiftUI 23 24 /// This is just a workaround for a SwiftUI bug 25 /// A presented sheet (SwiftUI view) doesn't always close when calling "dismiss()" provided by @Environment(\.dismiss), 26 /// so we are walking the view stack to find the top presentedViewController (UIKit) and dismiss it. 27 extension View { 28 @MainActor @discardableResult 29 func dismissTop(_ stack: CallStack? = nil, animated: Bool = true) -> Bool { 30 let windows = UIApplication.shared.connectedScenes.compactMap { 31 // TODO: iPad might have more than 1 window 32 // TODO: see UIScreen+screenSize for keyWindow 33 ($0 as? UIWindowScene)?.keyWindow 34 } 35 if var topController = windows.first?.rootViewController { 36 var gotPresented = false 37 var currentController = topController 38 while let presentedViewController = currentController.presentedViewController { 39 currentController = presentedViewController 40 gotPresented = true 41 } 42 if gotPresented { 43 currentController.dismiss(animated: animated) 44 return true 45 } else if let navController = Self.findNavigationController(viewController: topController) { 46 navController.popToRootViewController(animated: animated) 47 return true 48 } else { 49 print("Yikes❗️ There is no navigationController!") 50 } 51 } else { 52 print("Yikes❗️ There is no window/rootViewController!") 53 } 54 return false 55 } 56 @MainActor static func findNavigationController(viewController: UIViewController?) -> UINavigationController? { 57 guard let viewController = viewController else { 58 return nil 59 } 60 61 if let tabBarController = viewController as? UITabBarController { 62 return findNavigationController(viewController: tabBarController.selectedViewController) 63 } 64 65 if let navigationController = viewController as? UINavigationController { 66 return navigationController 67 } 68 69 for childViewController in viewController.children { 70 if let found = findNavigationController(viewController: childViewController) { 71 return found 72 } 73 } 74 75 return nil 76 } 77 } 78 // MARK: - 79 extension View { 80 @MainActor 81 static func endEditing() { 82 UIApplication.shared.windows.forEach { $0.endEditing(true) } 83 } 84 }