问题
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!
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)
}
}
}
回答1:
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!
回答2:
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!!!
回答3:
For me it happened because I had a view behind the tabbar. when I made the view hidden at the time of the animation it solved the problem
来源:https://stackoverflow.com/questions/25998364/navigation-bar-blinks-when-animating-for-the-first-time