gpt4 book ai didi

ios - 如何将 UINavigationControllerDelegate 连接到导航 Controller ?

转载 作者:行者123 更新时间:2023-11-30 12:12:13 25 4
gpt4 key购买 nike

答案:简单语法错误 - 设置委托(delegate)时,它应该读取 SomeNavigationController.delegate = self,而不是 SomeNavigationController.navigationController?.delegate = self

下面是创建导航 Controller 的基本 View Controller 。我将 self 声明为它的委托(delegate),并在该 View Controller 的扩展中遵守委托(delegate)的协议(protocol),并创建一个对象作为自定义动画师。但此导航堆栈中的 View Controller 不采用此动画;我错过了什么?

此导航堆栈的 Root View Controller 中或在此堆栈中推送和弹出的 View Controller 中是否需要存在任何代码?或者所有自定义动画代码都属于这里(在创建导航 Controller 的 View Controller 中)而我只是缺少一些东西?


创建 UINavigationController 的 View Controller

class BaseViewController: UIViewController {

override func loadView() {
buildNavigationController()
}

func buildNavigationController() {
let SomeNavigationControllerRoot = SomeViewController()
let SomeNavigationController = UINavigationController(rootViewController: SomeNavigationControllerRoot)
SomeNavigationController.delegate = self
view.addSubview(SomeNavigationController.view)
}

}

extension BaseViewController: UINavigationControllerDelegate {

func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return CustomAnimator()
}

}

class CustomAnimator: NSObject, UIViewControllerAnimatedTransitioning {

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 1.0
}


func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

let containerView = transitionContext.containerView
let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)

containerView.addSubview(toViewController!.view)

toViewController!.view.alpha = 0.0

UIView.animate(withDuration: 1.0, animations: {
toViewController!.view.alpha = 1.0
}, completion: { finished in
let cancelled = transitionContext.transitionWasCancelled
transitionContext.completeTransition(!cancelled)
})

}

}



UINavigationController 堆栈中的 Root View Controller

class SomeViewController: UIViewController, UINavigationControllerDelegate {

func pushToNext() {
let randomViewController = randomViewController()
navigationController?.pushViewController(randomViewController, animated: true)
}

}

最佳答案

如果你想使用自定义转换,你需要应用transitioningDelegate将 View Controller 的transitioningDelegate设置为您的自定义类并返回自定义动画 Controller 。

关于ios - 如何将 UINavigationControllerDelegate 连接到导航 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45909010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com