diff --git a/BottomSheetDemo/Sources/User Interface/Screens/Root/RootViewController.swift b/BottomSheetDemo/Sources/User Interface/Screens/Root/RootViewController.swift index 67317f2..678daee 100644 --- a/BottomSheetDemo/Sources/User Interface/Screens/Root/RootViewController.swift +++ b/BottomSheetDemo/Sources/User Interface/Screens/Root/RootViewController.swift @@ -54,7 +54,14 @@ final class RootViewController: UIViewController { let viewController = ResizeViewController(initialHeight: 300) presentBottomSheetInsideNavigationController( viewController: viewController, - configuration: .default + configuration: .default, + canBeDismissed: { + // return `true` or `false` based on your business logic + true + }, + dismissCompletion: { + // handle dismiss completion if user closed bottom sheet by a gesture + } ) } } diff --git a/README.md b/README.md index 4266afc..86f4c95 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,14 @@ presentBottomSheet( cornerRadius: 10, pullBarConfiguration: .visible(.init(height: 20)), shadowConfiguration: .init(backgroundColor: UIColor.black.withAlphaComponent(0.6)) - ) + ), + canBeDismissed: { + // return `true` or `false` based on your business logic + true + }, + dismissCompletion: { + // handle dismiss completion if user closed bottom sheet by a gesture + } ) ``` diff --git a/Sources/BottomSheet/Core/Extensions/UIViewController+Convenience.swift b/Sources/BottomSheet/Core/Extensions/UIViewController+Convenience.swift index 2e69656..a0015bf 100644 --- a/Sources/BottomSheet/Core/Extensions/UIViewController+Convenience.swift +++ b/Sources/BottomSheet/Core/Extensions/UIViewController+Convenience.swift @@ -69,7 +69,12 @@ public final class DefaultBottomSheetModalDismissalHandler: BottomSheetModalDism } public func performDismissal(animated: Bool) { - presentingViewController?.presentedViewController?.dismiss(animated: animated, completion: dismissCompletion) + if let presentedViewController = presentingViewController { + presentedViewController.dismiss(animated: animated, completion: dismissCompletion) + } else { + // User dismissed view controller by swipe-gesture, dismiss handler wasn't invoked + dismissCompletion?() + } } } @@ -106,8 +111,18 @@ public extension UIViewController { present(viewController, animated: true, completion: nil) } - func presentBottomSheetInsideNavigationController(viewController: UIViewController, configuration: BottomSheetConfiguration) { + func presentBottomSheetInsideNavigationController( + viewController: UIViewController, + configuration: BottomSheetConfiguration, + canBeDismissed: @escaping (() -> Bool) = { true }, + dismissCompletion: (() -> Void)? = nil + ) { let navigationController = BottomSheetNavigationController(rootViewController: viewController, configuration: configuration) - presentBottomSheet(viewController: navigationController, configuration: configuration) + presentBottomSheet( + viewController: navigationController, + configuration: configuration, + canBeDismissed: canBeDismissed, + dismissCompletion: dismissCompletion + ) } }