简体   繁体   English

UINavigationControllerDelegate不起作用

[英]UINavigationControllerDelegate doesn't work

Update to correct version, since have got the answer. 已得到答案,请更新至正确版本。

I want to do fade in/fade out animation when navigation view controller pop up /push, then I implement a BaseViewController : 当导航视图控制器弹出/ push时,我想做淡入/淡出动画,然后实现BaseViewController

class BaseViewController: UIViewController, UINavigationControllerDelegate {

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.delegate = self
    }


    func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {

        if (operation == UINavigationControllerOperation.Push) {
            return FadeInAnimator()
        }

        if (operation == UINavigationControllerOperation.Pop) {
            return FadeOutAnimator()
        }
        return nil;
    }
}

and FadeInAnimator , FadeOutAnimator : FadeInAnimatorFadeOutAnimator

class FadeInAnimator: NSObject, UIViewControllerAnimatedTransitioning {

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

        let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
        transitionContext.containerView()?.addSubview(toViewController!.view)
        toViewController?.view.alpha = 0.0
        UIView.animateWithDuration(self.transitionDuration(transitionContext), animations: { () -> Void in
            toViewController?.view.alpha = 1.0
        }) { (finished) -> Void in
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
        }
    }
}

class FadeOutAnimator: NSObject, UIViewControllerAnimatedTransitioning {
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
        let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
        transitionContext.containerView()?.insertSubview((toViewController?.view)!, belowSubview: (fromViewController?.view)!)

        UIView.animateWithDuration(self.transitionDuration(transitionContext), animations: { () -> Void in
            fromViewController?.view.alpha = 0.0
        }) { (finished) -> Void in
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
    }
}

Then, I add 2 view controllers and embed navigation view controller, ViewControllerA and ViewControllerB : 然后,我添加2个视图控制器并嵌入导航视图控制器ViewControllerAViewControllerB

class ViewControllerA: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func pressButton(sender: UIButton) {

        if let vc = create(ViewControllerB) {
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }  
}

class ViewControllerB: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

press button on ViewControllerA will create and jump to ViewControllerB , then press navigation bar item "back" on ViewControllerB will back to ViewControllerA . 在按下按钮ViewControllerA将创建并跳转到ViewControllerB ,然后按导航栏项目的“背”上ViewControllerB会回到ViewControllerA

But it doesn't work, when press button, the ViewControllerA show ViewControllerB , then show ViewControllerA again. 但这是行不通的,当按下按钮时, ViewControllerA显示ViewControllerB ,然后再次显示ViewControllerA

any suggestion? 有什么建议吗? my xcode is 7.1 and run on iPad Air 2 simulator. 我的xcode是7.1,可以在iPad Air 2模拟器上运行。

in your FadeInAnimator at animation completion block 在动画完成块的FadeInAnimator中

change this 改变这个

transitionContext.completeTransition(transitionContext.transitionWasCancelled())

to

transitionContext.completeTransition(!transitionContext.transitionWasCancelled())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UINavigationControllerDelegate 在返回两次时不起作用 - UINavigationControllerDelegate doesn't work when back twice 无法弄清楚如何在UIViewController中一起使用UINavigationControllerDelegate和pushViewController - Can't figure out how to use UINavigationControllerDelegate and pushViewController together in a UIViewController 无法分配类型“ Class”的值 <T> &#39;键入&#39;协议 <UIImagePickerControllerDelegate, UINavigationControllerDelegate> &#39; - Cannot assign value of type 'Class<T>' to type 'protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>?' 无法以编程方式设置UINavigationControllerDelegate(但可以通过故事板设置) - Can't set UINavigationControllerDelegate programatically (but can set via storyboard) UIViewControllerContextTransitioning 和 UINavigationControllerDelegate - UIViewControllerContextTransitioning and UINavigationControllerDelegate 条件不起作用 - Condition doesn't work NSLocalizedStringFromTable不起作用 - NSLocalizedStringFromTable doesn't work watchValueForKeyPath不起作用 - observeValueForKeyPath doesn't work CocoaPods不起作用 - CocoaPods doesn't work UIViewContentModeScaleAspectFit不起作用 - UIViewContentModeScaleAspectFit doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM