Skip to content

Commit

Permalink
Fix animations not working on iOS 17.1 (#110)
Browse files Browse the repository at this point in the history
Fixes and closes #109.
  • Loading branch information
davdroman authored Oct 27, 2023
1 parent 4a1397f commit 68d1a6c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 35 deletions.
80 changes: 54 additions & 26 deletions Sources/NavigationTransitions/NavigationTransition+UIKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ extension UINavigationController {
_ transition: AnyNavigationTransition,
interactivity: AnyNavigationTransition.Interactivity = .default
) {
backDeploy96852321()

if defaultDelegate == nil {
defaultDelegate = delegate
}
Expand All @@ -147,6 +145,30 @@ extension UINavigationController {
customDelegate.transition = transition
}

swizzle(
UINavigationController.self,
#selector(UINavigationController.pushViewController),
#selector(UINavigationController.pushViewController_animateIfNeeded)
)

swizzle(
UINavigationController.self,
#selector(UINavigationController.popViewController),
#selector(UINavigationController.popViewController_animateIfNeeded)
)

swizzle(
UINavigationController.self,
#selector(UINavigationController.popToViewController),
#selector(UINavigationController.popToViewController_animateIfNeeded)
)

swizzle(
UINavigationController.self,
#selector(UINavigationController.popToRootViewController),
#selector(UINavigationController.popToRootViewController_animateIfNeeded)
)

#if !os(tvOS) && !os(visionOS)
if defaultEdgePanRecognizer.strongDelegate == nil {
defaultEdgePanRecognizer.strongDelegate = NavigationGestureRecognizerDelegate(controller: self)
Expand Down Expand Up @@ -196,28 +218,6 @@ extension UINavigationController {
#endif
}

private func backDeploy96852321() {
func forceAnimatedPopToViewController() {
swizzle(
UINavigationController.self,
#selector(UINavigationController.popToViewController),
#selector(UINavigationController.popToViewController_forceAnimated)
)
}

if #available(iOS 16.2, macCatalyst 16.2, tvOS 16.2, *) {} else {
#if targetEnvironment(macCatalyst)
let major = ProcessInfo.processInfo.operatingSystemVersion.majorVersion
let minor = ProcessInfo.processInfo.operatingSystemVersion.minorVersion
if (major, minor) < (13, 1) {
forceAnimatedPopToViewController()
}
#else
forceAnimatedPopToViewController()
#endif
}
}

@available(tvOS, unavailable)
@available(visionOS, unavailable)
private func exclusivelyEnableGestureRecognizer(_ gestureRecognizer: UIPanGestureRecognizer?) {
Expand All @@ -232,8 +232,36 @@ extension UINavigationController {
}

extension UINavigationController {
@objc private func popToViewController_forceAnimated(_ viewController: UIViewController, animated: Bool) -> [UIViewController]? {
popToViewController_forceAnimated(viewController, animated: true)
@objc private func pushViewController_animateIfNeeded(_ viewController: UIViewController, animated: Bool) {
if let transitionDelegate = customDelegate {
pushViewController_animateIfNeeded(viewController, animated: transitionDelegate.transition.animation != nil)
} else {
pushViewController_animateIfNeeded(viewController, animated: animated)
}
}

@objc private func popViewController_animateIfNeeded(animated: Bool) -> UIViewController? {
if let transitionDelegate = customDelegate {
return popViewController_animateIfNeeded(animated: transitionDelegate.transition.animation != nil)
} else {
return popViewController_animateIfNeeded(animated: animated)
}
}

@objc private func popToViewController_animateIfNeeded(_ viewController: UIViewController, animated: Bool) -> [UIViewController]? {
if let transitionDelegate = customDelegate {
return popToViewController_animateIfNeeded(viewController, animated: transitionDelegate.transition.animation != nil)
} else {
return popToViewController_animateIfNeeded(viewController, animated: animated)
}
}

@objc private func popToRootViewController_animateIfNeeded(animated: Bool) -> UIViewController? {
if let transitionDelegate = customDelegate {
return popToRootViewController_animateIfNeeded(animated: transitionDelegate.transition.animation != nil)
} else {
return popToRootViewController_animateIfNeeded(animated: animated)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,18 @@ final class NavigationTransitionDelegate: NSObject, UINavigationControllerDelega
var transition: AnyNavigationTransition
private weak var baseDelegate: UINavigationControllerDelegate?
var interactionController: UIPercentDrivenInteractiveTransition?
private var initialAreAnimationsEnabled: Bool?

init(transition: AnyNavigationTransition, baseDelegate: UINavigationControllerDelegate?) {
self.transition = transition
self.baseDelegate = baseDelegate
}

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
if navigationController.transitionCoordinator != nil {
self.initialAreAnimationsEnabled = UIView.areAnimationsEnabled
UIView.setAnimationsEnabled(transition.animation != nil)
}
baseDelegate?.navigationController?(navigationController, willShow: viewController, animated: animated)
}

func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
baseDelegate?.navigationController?(navigationController, didShow: viewController, animated: animated)
if let initialAreAnimationsEnabled {
UIView.setAnimationsEnabled(initialAreAnimationsEnabled)
self.initialAreAnimationsEnabled = nil
}
}

func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
Expand Down

0 comments on commit 68d1a6c

Please sign in to comment.