iOS7 Facebook NavigationBar Behavior

2019-05-05 14:14发布

问题:

My App has more or less the same navigation concept as Facebook's / Instagram's iOS7 Apps:

A ContainerViewController with 5 tabs, each of which has a NavigationController as it's rootViewController.

I'm now trying to reproduce Facebook's navigationBar behavior for the rootViewController of the first tab's navigationController (-> the first 'real' VC, not just a container like NavVC).

I was able to implement a hiding/showing navigationBar using the UIScrollView Delegate methods (scrollViewDidScroll:, scrollViewWillBeginDragging:, scrollViewDidEndDragging:)

Note: The frame.origin.y of the navigationBar is manually offset to the top. I'm NOT using

    [self.navigationController setNavigationBarHidden:]

It's really smooth so far. But I'm facing 2 problems i can't think of a solution for:

When I'm pushing a new ViewController onto the first tab's navigation stack, the navigationBar stays hidden.

To solve this problem I do this in the pushed ViewController's viewWillAppear: method :

    CGRect navBarFrame = self.navigationController.navigationBar.frame;
    navBarFrame.origin.y = 20.0;
    [UIView animateWithDuration:0.3 animations:^{
        [self.navigationController.navigationBar setFrame:navBarFrame];
    }];

This animation is matched properly to the navigationController push animation by iOS.

Problem: when I'm navigating back (popping the pushed VC), the navigationBar stays visible.

I was able to solve this problem by creating and setting some properties on both ViewControllers but it is a huge mess and it will get even more hacky, because I'll have to push 3 or 4 different ViewController types onto this navigation stack.

My Question (finally -.-) :

How can I achieve a pushing / popping behavior similar to Facebook's ?

This is the behavior I'm looking for: a clean transition, where the 'old' navigationBar stays hidden and the pushed viewController's bar is shown.

Note: the screenshot was taken during the new 'slide from left to right to go back' iOS7 gesture.

The second question: Is there a way to implement a global handler in order to create this behavior in any viewController without having to implement the scrollDelegate logic in every single one of them? I'm thinking of a delegate object which conforms to UIScrollView Delegate.

Thanks for your help :)

回答1:

Long story short: I built my own custom container for that. It provides the ability to switch between tabs, as well as pushing new ViewControllers on each tab. Kind of like a hybrid between UINavigationController and UITabBarController.

If you need a more detailed answer on that please let me know.



回答2:

I wrote a custom sliding animation transitioning subclass of UINavigationController. It's behavior is similar to Facebook/Instagram. Previous controller navigationBar stays visible seems OK.

Please checkout this CCSlideNavigationTransition. Hopes helpful!