过渡后导航栏跳跃

时间:2016-05-24 01:21:52

标签: ios swift uikit uitabbarcontroller transition

我正在创建一个使用标签栏在视图控制器之间导航的应用。我想添加一个过渡效果,当按下标签按钮时,它会在每个视图之间交叉溶解。我已使用UIView.transitionFromView实现了此转换,但导航栏在转换期间未按预期工作。在第一次转换到视图期间,导航栏显示得太高,但一旦转换完成就会跳回原位。但是,下次切换到同一视图时,导航栏在转换期间和转换后都处于正确的位置。

我已经看到了解决自定义动画问题的答案here,但我无法弄清楚如何让它与我当前的实现一起使用。

我的问题 我已经看到通过将视图强制降低几个点(44分)来解决问题的答案,但有没有办法在不直接更改积分的情况下执行此操作?这可能是第一次使用,但是当任何视图转换到第二次时问题会自行解决,因此如果更改点,视图会过低。

以下是标签栏控制器和转换的代码:

import UIKit

class MainTabBarViewController: UITabBarController, UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // Method used to detect when a tab bar button has been tapped
    func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {

        // Creating the 'to' and 'from' views for the transition
        let fromView = tabBarController.selectedViewController!.view
        let toView = viewController.view

        if fromView == toView {
            // If views are the same, then don't do a transition
            return false
        }

        self.view.userInteractionEnabled = false
        UIView.transitionFromView(fromView, toView: toView, duration: 2.0, options: .TransitionCrossDissolve, completion: nil)
        self.view.userInteractionEnabled = true

        return true

    }

}

这就是问题所在:

iPhone Recording

2 个答案:

答案 0 :(得分:2)

您可以尝试使用以下代码:

import UIKit

class MainTabBarViewController: UITabBarController, UITabBarControllerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
    self.delegate = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

// Method used to detect when a tab bar button has been tapped
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {

    // Creating the 'to' and 'from' views for the transition
    let fromView = tabBarController.selectedViewController!.view
    let toView = viewController.view

    if fromView == toView {
        // If views are the same, then don't do a transition
        return false
    }

    self.view.userInteractionEnabled = false

    if let window = fromView.window {
        let overlayView = UIScreen.mainScreen().snapshotViewAfterScreenUpdates(false)
        viewController.view.addSubview(overlayView)
        UIView.transitionFromView(fromView, toView: toView, duration: 2.0, options: .TransitionCrossDissolve, completion: { (finish) in
            window.rootViewController = viewController
            UIView.animateWithDuration(0.4, delay: 0.0, options: .TransitionCrossDissolve, animations: {
                overlayView.alpha = 0
                }, completion: { (finish) in
                    overlayView.removeFromSuperview()
                })
            })
        }

        self.view.userInteractionEnabled = true

        return true
    }
}

答案 1 :(得分:0)

就我而言,在过渡解决此问题之前,请致电toView.layoutIfNeeded()