TimOliver / TONavigationBar

Replicating the 'clear' navigation bar style of the iOS 12 Apple TV app.
MIT License
250 stars 24 forks source link

to_navigationBar is nil but navigationBar is not? #10

Open wiencheck opened 5 years ago

wiencheck commented 5 years ago

I encountered a problem with this framework, I might be using it wrong somehow but I followed the instructions. I create my navigation controller like this:

self.rootViewController = UINavigationController(navigationBarClass: TONavigationBar.self, toolbarClass: nil) I'm using the Coordinator patter here so rootViewController is declared as var rootViewController: UINavigationController

In my detail view controller the navigation bar does not hide, the console output is quite strange, see for yourself

Zrzut ekranu 2019-07-02 o 07 44 40
wiencheck commented 5 years ago

Maybe it would be helpful to mention that the navigation controller is not the root controller of the app's window, unlike in the Example app. I don't know if it has to do with anything, just fyi

wiencheck commented 5 years ago

I found the issue however it will be a pain in the arsenal to solve in my project. I played a little in a test app and found out that if the navigation controller with the to_navigation is not a top navigation controller, it doesn't work as expected. I use a similar architecture in my app to this:

Zrzut ekranu 2019-07-04 o 11 02 19

Navigation Controller -> Container View -> Navigation Controller -> Plain View Controller -> View Controller with Table View and transparent to_navigationBar

TimOliver commented 5 years ago

G'day Adam! Thanks so much for the thorough description of your use case!

Hmm, I wonder if it is indeed a bug with the way I set up the logic for to_navigationBar.

The logic for it is here: https://github.com/TimOliver/TONavigationBar/blob/master/TONavigationBar/TONavigationBar.m#L376

It basically starts at navigation controller you call it on, and works its way up the chain to find the last visible one. This logic might not be correct (In fact, I'm trying to remember why I even did it like that to begin with. 😅)

In any case, the point of that method is to be only a convenience method so you don't have to manually typecast the UINavigationController's navigation bar property every single time.

Since you're writing it in Swift, it might be worth writing your own extension for now, and see if you can get it working that way.

Without actually testing this code at all, I would imagine implementing the same method in Swift would look like:


extension UINavigationController {
   public var toNavigationBar: TONavigationBar? {
      if let navbar = self.navigationBar as? TONavigationBar {
         return navbar
      }

      return nil
   }
}

See if that works for you. If so, I might have to re-write how that method works. :)

Thanks!

wiencheck commented 5 years ago

I changed the TONavigationBar.m a bit to this

- (TONavigationBar *)to_navigationBar
{
    UINavigationController *navigationController = self;
    UIViewController *controller = self;
    do {
        if ([controller isKindOfClass:UINavigationController.class]) {
            navigationController = (UINavigationController*)controller;

            if ([navigationController.navigationBar isKindOfClass:[TONavigationBar class]]) {
                break;
            }

        }

        controller = controller.parentViewController;
    } while (controller != nil);

    if ([navigationController.navigationBar isKindOfClass:[TONavigationBar class]]) {
        return (TONavigationBar *)navigationController.navigationBar;
    }

    return nil;
}

@end

I needed a solution quickly because it was starting to drive me insane and this did the trick :D

TimOliver commented 5 years ago

Hahaha glad to hear you got it working.

I probably should fix this. So we can leave this issue to remind me to get around to doing this.

Thanks for letting me know!