|
7 | 7 |
|
8 | 8 | import SwiftUI |
9 | 9 |
|
10 | | -struct SwipeBackModifier: UIViewControllerRepresentable { |
| 10 | +struct EnableSwipeBack: ViewModifier { |
| 11 | + func body(content: Content) -> some View { |
| 12 | + content |
| 13 | + .background(EnableSwipeBackRepresentable()) |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +private struct EnableSwipeBackRepresentable: UIViewControllerRepresentable { |
11 | 18 | func makeUIViewController(context: Context) -> UIViewController { |
12 | | - let viewController = UIViewController() |
| 19 | + let controller = UIViewController() |
13 | 20 | DispatchQueue.main.async { |
14 | | - if let navController = viewController.navigationController { |
15 | | - navController.interactivePopGestureRecognizer?.delegate = context.coordinator |
16 | | - navController.interactivePopGestureRecognizer?.isEnabled = true |
| 21 | + if let nav = controller.parentNavigationController { |
| 22 | + nav.interactivePopGestureRecognizer?.delegate = context.coordinator |
| 23 | + nav.interactivePopGestureRecognizer?.isEnabled = true |
17 | 24 | } |
18 | 25 | } |
19 | | - return viewController |
| 26 | + return controller |
20 | 27 | } |
21 | | - |
22 | | - func updateUIViewController(_ uiViewController: UIViewController, context: Context) { } |
23 | | - |
| 28 | + |
| 29 | + func updateUIViewController(_ uiViewController: UIViewController, context: Context) {} |
| 30 | + |
24 | 31 | func makeCoordinator() -> Coordinator { |
25 | 32 | Coordinator() |
26 | 33 | } |
27 | | - |
| 34 | + |
28 | 35 | class Coordinator: NSObject, UIGestureRecognizerDelegate { |
29 | 36 | func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { |
| 37 | + // 뒤로가기 제스처가 항상 동작하도록 허용 |
30 | 38 | return true |
31 | 39 | } |
32 | | - |
33 | | - func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool { |
34 | | - return otherGestureRecognizer is UIPanGestureRecognizer && |
35 | | - !(gestureRecognizer is UIScreenEdgePanGestureRecognizer) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// UINavigationController를 안전하게 찾는 extension |
| 44 | +private extension UIViewController { |
| 45 | + var parentNavigationController: UINavigationController? { |
| 46 | + var parentVC = self.parent |
| 47 | + while parentVC != nil { |
| 48 | + if let nav = parentVC as? UINavigationController { |
| 49 | + return nav |
| 50 | + } |
| 51 | + parentVC = parentVC?.parent |
36 | 52 | } |
| 53 | + return nil |
37 | 54 | } |
38 | 55 | } |
39 | 56 |
|
40 | 57 | extension View { |
41 | 58 | func enableSwipeBack() -> some View { |
42 | | - self.background(SwipeBackModifier()) |
| 59 | + self.modifier(EnableSwipeBack()) |
43 | 60 | } |
44 | 61 | } |
0 commit comments