Navigation bar flashes when animating for the first time
I created my own type of tabbar control, but I have a problem with animation. When I animate the views on a tab click, the nav bar turns completely black (should be red) and then after the animation finishes flashing back to red. My setup and code is below.
(The answers are either swift or objective-c helpful since tranlastion is easy)
Thanks in advance!
red: navigation bar
blue: navigation display view
gray: tab bar
burgundy: Display the tab bar (this is the part that transitions / animates)
I change / 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)
}
}
}
source to share
I had a similar problem with UINavigationController and it fixed it, try:
Just before this line:
UIView.transitionFromView(self.displayView, toView: selectedController!.view, duration: self.animationDuration, options: self.animationOptions, completion: {finished in
Add selectedController.view to the view hierarchy like this (sorry Obj-C code ).
[self.displayView.superview addSubview:selectedController.view];
Let me know if it works :) GOOD LUCK!
source to share
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
guard let fromView = selectedViewController?.view, let toView = viewController.view else
{
return false
}
if fromView != toView
{
fromView.addSubview(toView)
UIView.transition(from: fromView, to: toView, duration: 0.15, options: UIView.AnimationOptions.transitionCrossDissolve, completion: nil)
}
return true
}
It worked for me!
source to share