Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Bar Blinks When Animating for the First Time

I have created my own tabbarcontroller of sorts, but am having an issue with animation. When I animate the views on tab click, the navigation bar turns completely black (should be red), then blinks back to red after the animation completes. My setup and code is below.

(Answers in either swift or objective-c are helpful since the tranlastion is easy)

Thanks in advance!

enter image description here

red: Navigation Bar

blue: Navigation Display View

grey: Tab Bar

burgundy: Tab Bar Display View (This is the part being transitioned/animated)

I transition/animate between my views with the below code.

 //Handles selection of a tab bar item
    func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {

        //Get controller for transition
        var selectedController = self.controllers[item.tag];

        //Only transition for new view
        if(self.childViewControllers[0] as UIViewController != selectedController!){
            //Prepare for controller transition
            selectedController!.viewWillAppear(false)
            self.currViewController?.viewWillDisappear(false)
            self.currViewController?.willMoveToParentViewController(nil)

            //Set new controller with animation
            selectedController!.view.frame = self.displayView.frame
            if(transitionAnimated){
                UIView.transitionFromView(self.displayView, toView: selectedController!.view, duration: self.animationDuration, options: self.animationOptions, completion: {finished in
                        self.setNewController(selectedController!)
                        self.animationCompletion(finished)
                })
            }
            else{
                self.displayView.removeFromSuperview()
                self.view.addSubview(selectedController!.view)
                setNewController(selectedController!);
            }
        }
        else{
            if(self.childViewControllers[0].isKindOfClass(UINavigationController)){
                (self.childViewControllers[0] as UINavigationController).popToRootViewControllerAnimated(true)
            }
        }
    }
like image 537
steventnorris Avatar asked Dec 26 '22 04:12

steventnorris


2 Answers

I had a similar problem with UINavigationController and this fixed it, try it:

Just before this line:

UIView.transitionFromView(self.displayView, toView: selectedController!.view, duration: self.animationDuration, options: self.animationOptions, completion: {finished in

Add the selectedController.view into the view hierarchy like this (Sorry Obj-C code) [self.displayView.superview addSubview:selectedController.view];

Let me know if it works :) GOOD LUCK!

like image 166
dev_jac Avatar answered Dec 28 '22 08:12

dev_jac


func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {

        guard let fromView = selectedViewController?.view, let toView = viewController.view else
        {
            return false
        }

        if fromView != toView
        {
            fromView.superview!.addSubview(toView)
            UIView.transition(from: fromView, to: toView, duration: 0.15, options: UIView.AnimationOptions.transitionCrossDissolve, completion: nil)
        }

        return true
    }

This worked for me!!!

like image 30
Saad Tahir Avatar answered Dec 28 '22 10:12

Saad Tahir