andreamazz / AMScrollingNavbar

Scrollable UINavigationBar that follows the scrolling of a UIScrollView
MIT License
6.05k stars 633 forks source link

Only works first time on a view controller #346

Closed aehlke closed 6 years ago

aehlke commented 6 years ago

Describe the bug I've implemented the library according to the readme. It works great the first time I navigate to the VC where I use this library. When I navigate back and forth again, I can see that navigationController.scrollingEnabled is true, but the navbar does not hide when I scroll anymore.

I have the following:

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        if let navigationController = navigationController as? ScrollingNavigationController, let activeWebViewController = activeWebViewController {
            print(navigationController.scrollingEnabled) // shows true
            navigationController.showNavbar(animated: true)
            navigationController.followScrollView(activeWebViewController.webView)
            print(navigationController.scrollingEnabled) // shows true
        }
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)

        if let navigationController = navigationController as? ScrollingNavigationController {
            navigationController.stopFollowingScrollView()

            // I tried adding these because I noticed they were missing, but it did not help.
            NotificationCenter.default.removeObserver(navigationController, name: UIApplication.willResignActiveNotification, object: nil)
            NotificationCenter.default.removeObserver(navigationController, name: UIWindow.didBecomeVisibleNotification, object: nil)
        }
    }

I also confirmed the VC deinitializes between navigating away and back to a new instance of it.

To Reproduce Steps to reproduce the behavior: iOS 12. I will try to create an example project.

Expected behavior I expect the navbar to hide again when I scroll after re-opening a VC.

Anything obvious jump out as a possible cause for this? I'm a bit lost about what to investigate at this point.

aehlke commented 6 years ago

I copied the source into my project so that I could more easily debug, and I see that the issue is that the self.scrollableView == nil check fails in followScrollView on the 2nd time I open the VC, despite it being a new instance of the VC and despite having previously called stopFollowingScrollView. I'll dig in further later.

andreamazz commented 6 years ago

Hey @aehlke were you able to reproduce the issue in the demo project by any chance?

aehlke commented 6 years ago

@andreamazz my current line of investigation is to step through the code in my own project, now that I imported AMScrollingNavbar's source and can see which variable is unexpectedly non-nil and causing the wrong code path to execute. I'll let you know when I can narrow down what's responsible.

aehlke commented 6 years ago

Well, simple:

override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        print(navigationController)
        [...]
}

prints nil. Odd, but must be something I'm doing wrong rather than the library. But perhaps a different recommendation is needed in the documentation. I'm still investigating but I see reports that this is possible under normal circumstances.

My solution for now is private weak var weakNavigationController: UINavigationController?