开发者

Disable animation of navigation bar

I am working on a navigation-based application. I have few View Controllers which I push in to Navigation Controller on different occasions. The following is the code I use to push new View Controller.

AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
[self.navigationController pushViewController:anotherViewController animated:YES];
[anotherViewController release];

One thing I noticed is that, when new view controller is pushed the navigation bar also animated (slided). I have a back button, title text and right button in navigation b开发者_如何学Pythonar. So it look weird when the navigation bar is animated.

Is there any way around to keep the navigation bar fixed and the view is only animated when a new view controller is pushed?


I tried a lot of different approaches but nothing worked. Finally I use a workaround:

1) Hand over the current view (self.view) of the first view controller to the second view controller 2) Do a pushViewController with e.g. a fade transition. Set the second view controller as delegate of the animation to inform the second controller if transition is finished 3) In viewDidLoad of second view controller save the view of the second view controller and set the view of the first one as self.view

Now the display should look like the content of the first view controller with the navigation bar (and toolbar if so) of the second one.

Finally perform in the second view controller (in delegate method of animation) the transition you want to do for the content e.g. flip.

Basically that works. I have still to resolve some issues with correct position of the view and restoring the view in the first view controller for my app.

But this should give you a hint at least.

Update: Fixed remaining issues. It was a bit tricky because the first view controller resides in a scrollview. Flipping out was not a big deal, but flipping back into a scroll view is a nasty thing.


Swift

Hard but feasible solution:

You need to use the UINavigationController delegate methods to find out when the UIViewController is being shown. Then for each UIViewController, need to make a boolean variable like isInitialized property, which help you to determine when the UIViewController is being pushed on the stack, or when it's being shown upon back of next view controller.

Your FirstViewController:

func navigationController(_ navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
        if viewController == self {
            if self.isInitialized {
                var navigationBarAnimation = CATransition()
                navigationBarAnimation.duration = 1.5
                navigationBarAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
                navigationBarAnimation.type = kCATransitionFade
                navigationBarAnimation.subtype = kCATransitionFade
                navigationBarAnimation.removedOnCompletion = true
                self.navigationController?.navigationBar?.layer?.addAnimation(navigationBarAnimation, forKey: nil)
                }
                else 
                {
                        self.isInitialized = true;
                }
        }
}

func navigationController(_ navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
        if viewController == self {
            if self.isInitialized {
                self.navigationController?.navigationBar?.layer?.removeAllAnimations()
            }
        }
}

Your SecondViewController:

func navigationController(_ navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
        if viewController == self {
            if !self.isInitialized {
                var navigationBarAnimation = CATransition()
                navigationBarAnimation.duration = 1.5
                navigationBarAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
                navigationBarAnimation.type = kCATransitionFade
                navigationBarAnimation.subtype = kCATransitionFade
                navigationBarAnimation.removedOnCompletion = true
                self.navigationController?.navigationBar?.layer?.addAnimation(navigationBarAnimation, forKey: nil)
                        self.isInitialized = true;
                }
        }
}

func navigationController(_ navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
        if viewController == self {
            if self.isInitialized {
                self.navigationController?.navigationBar?.layer?.removeAllAnimations()
            }
        }
}


The navigation bar hides while viewing images in Apple’s Photo app to provide a better view of the image. Hiding the navigation bar is easy. While not exactly the same as the Photo app this technique will hide and show the navigation bar, with an option to animate the transition.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜