Completion block for popViewController

后端 未结 18 1595
离开以前
离开以前 2020-12-04 06:43

When dismissing a modal view controller using dismissViewController, there is the option to provide a completion block. Is there a similar equivalent for

18条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-04 07:30

    Cleaned up Swift 4 version based on this answer.

    extension UINavigationController {
        func pushViewController(_ viewController: UIViewController, animated: Bool, completion: @escaping () -> Void) {
            self.pushViewController(viewController, animated: animated)
            self.callCompletion(animated: animated, completion: completion)
        }
    
        func popViewController(animated: Bool, completion: @escaping () -> Void) -> UIViewController? {
            let viewController = self.popViewController(animated: animated)
            self.callCompletion(animated: animated, completion: completion)
            return viewController
        }
    
        private func callCompletion(animated: Bool, completion: @escaping () -> Void) {
            if animated, let coordinator = self.transitionCoordinator {
                coordinator.animate(alongsideTransition: nil) { _ in
                    completion()
                }
            } else {
                completion()
            }
        }
    }
    

提交回复
热议问题