-1

Hello guys I have a problem to close my NavigationController. This piece of code is not working for me. When I try to call it in my ViewController

   @IBAction func backButtonPressed(_ sender: UIBarButtonItem) {
        self.navigationController?.dismiss(animated: true, completion: nil)
    }

This is what I am doing in my first ViewController I just open the ProfileViewController which have a custom transition.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "Profile") as? NavigationControllerTransition
controller?.modalPresentationStyle = .custom
controller?.transitioningDelegate = self
controller?.interactor = self.interactor
controller?.animatorDirection.direction = .right
self.animatedTransition.animatorDirection.direction = .right
self.present(controller!, animated: true, completion: {})

I have a custom NavigationController Class which calls NavigationControllerTransition to make it happen to swipe from the edge from the screen for the custom transition.

class NavigationControllerTransition: UINavigationController {

    var interactor: Interactor?
    var animatorDirection = AnimatorDirection()

    override func viewDidLoad() {
        super.viewDidLoad()

        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(NavigationControllerTransition.handleTap(sender:)))
        self.view.addGestureRecognizer(panGesture)

    }

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

    func handleTap(sender: UIPanGestureRecognizer) {

        let percentThreshold: CGFloat = 0.3
        let translation = sender.translation(in: view)

        var horitonzalMovement: CGFloat = 0.0

        var downwardMovementPercent: Float = 0.0

        if self.animatorDirection.direction == .left {
            horitonzalMovement = -(translation.x / view.bounds.width)
            let downwardMovement = fmaxf(Float(horitonzalMovement), 0.0)
            downwardMovementPercent = fminf(downwardMovement, 1.0)

        } else {
            horitonzalMovement = +(translation.x / view.bounds.width)
            let downwardMovement = fmaxf(Float(horitonzalMovement), 0.0)
            downwardMovementPercent = fminf(downwardMovement, 1.0)

        }

        let progress = CGFloat(downwardMovementPercent)
        guard let interactor = interactor else { return }

        switch sender.state {
        case .began:
            interactor.hasStarted = true
            dismiss(animated: true, completion: nil)
        case .changed:
            interactor.shouldFinish = progress > percentThreshold
            interactor.update(progress)
        case .cancelled:
            interactor.hasStarted = false
            interactor.cancel()
        case .ended:
            interactor.hasStarted = false
            interactor.shouldFinish ? interactor.finish() : interactor.cancel()
        default: break
        }
    }
}

So I can swipe to open or close my ViewController and the transition works great. The only problem is when I try to close my ViewController using a UIButton. The NavigationController will no dismiss and freeze. Any tips ? :(

BilalReffas
  • 8,132
  • 4
  • 50
  • 71

1 Answers1

1

I'm going to assume that you're just trying to dismiss the ViewController and not the navigation controller itself. In that case your IBAction should look like this.

@IBAction func backButtonPressed(_ sender: UIBarButtonItem) {
    self.dismissViewControllerAnimated(true, completion: nil)
    // Technically you don't even need the self here. 
    // In swift self is implicit in most cases. 
}

Think of a Navigation controller as handling a stack of views. When you segue to a new view within the navigation controller, you add a new view to the stack. When you dismiss you pop one off of the stack. Also never segue to go back, always dismiss. If you fail to dismiss a view controller on the stack the view controller will just remain in memory.

Here is a good tutorial on the subject.

If you need to pass data back to the previous view one technique is called an unwind segue. Read up on that here.

Hope this helps!

Donovan King
  • 855
  • 1
  • 6
  • 18