Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to_navigationBar is nil but navigationBar is not? #10

Open
wiencheck opened this issue Jul 2, 2019 · 5 comments
Open

to_navigationBar is nil but navigationBar is not? #10

wiencheck opened this issue Jul 2, 2019 · 5 comments
Labels
bug Something isn't working

Comments

@wiencheck
Copy link

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
Copy link
Author

wiencheck commented Jul 3, 2019

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
Copy link
Author

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
Copy link
Owner

TimOliver commented Jul 4, 2019

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!

@TimOliver TimOliver added the bug Something isn't working label Jul 4, 2019
@wiencheck
Copy link
Author

wiencheck commented Jul 4, 2019

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
Copy link
Owner

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants