导航栏在第一次动画时闪烁

时间:2014-09-23 14:57:49

标签: objective-c swift uinavigationcontroller uitabbar uiviewanimation

我创建了自己的各种tabbarcontroller,但是我遇到了动画问题。当我在选项卡上单击时为视图设置动画时,导航栏将完全变为黑色(应为红色),然后在动画完成后闪烁回红色。我的设置和代码如下。

(swift或objective-c中的答案很有用,因为转换很容易)

提前致谢!

enter image description here

红色:导航栏

蓝色:导航显示视图

灰色:标签栏

勃艮第:标签栏显示视图(这是正在转换/动画的部分)

我使用以下代码在视图之间转换/动画。

 //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)
            }
        }
    }

3 个答案:

答案 0 :(得分:9)

我遇到了与UINavigationController类似的问题,并修复了它,尝试一下:

就在这一行之前:

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

将selectedController.view添加到视图层次结构中(对不起 Obj-C 代码) [self.displayView.superview addSubview:selectedController.view];

让我知道它是否有效:)祝你好运!

答案 1 :(得分:1)

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
    }

这对我有用!

答案 2 :(得分:0)

对我来说,发生这种情况是因为我在标签栏后面有一个视图。当我在动画时将视图隐藏时,它解决了问题