[英]Navigation Bar Jumping After Transition
我正在制作一個使用標簽欄在視圖控制器之間導航的應用程序。 我想添加一個過渡效果,當按下選項卡按鈕時,它會在每個視圖之間交叉溶解。 我已經使用UIView.transitionFromView
實現了這個轉換,但是在轉換過程中導航欄沒有按預期工作。 在第一次轉換到視圖期間,導航欄顯示得太高,但一旦轉換完成就會跳回原位。 但是,下次切換到同一視圖時,導航欄在轉換期間和之后都位於正確的位置。
我在這里看到了一個解決自定義動畫問題的答案,但我不知道如何讓它與我當前的實現一起工作。
我的問題我已經看到通過將視圖向下幾個點(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
}
}
這是問題的樣子:
您可以嘗試使用以下代碼:
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
}
}
就我而言,在轉換修復問題之前調用toView.layoutIfNeeded()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.