我使用TabBarControllerDelegate方法shouldSelectViewController
在TabBarController中进行转换。我收到了以下警告
Unbalanced calls to begin/end appearance transitions for <UINavigationController: 0x1804dcf0>.
我认为这是因为我在目标ViewController上调用viewWillAppear(true)
(在下面用***标记)的行(这样可以平滑过渡到新控制器)。
我看到this question和this other question相似但不完全相同。我想我应该告诉屏幕上的当前View Controller他将被删除(因为当添加标记的行时这个警告开始出现),但我不确定如何。
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
let controllerIndex: Int = find( (self.viewControllers as! [UIViewController]), viewController)!
if(controllerIndex == tabBarController.selectedIndex) {
return false
}
// Get the views
let fromView: UIView = tabBarController.selectedViewController!.view
let toView: UIView = (tabBarController.viewControllers as! [UIViewController])[controllerIndex].view
***
// Tell the destination view controller know it will appear on screen
(tabBarController.viewControllers as! [UIViewController])[controllerIndex].viewWillAppear(true)
***
let viewSize = fromView.frame
let scrollRight = controllerIndex > tabBarController.selectedIndex;
// Add the to view to the tab bar view.
fromView.superview?.addSubview(toView)
// Position it off screen.
let screenWidth = UIScreen.mainScreen().bounds.size.width
toView.frame = CGRectMake(
scrollRight ? screenWidth : -screenWidth,
viewSize.origin.y,
screenWidth,
viewSize.size.height
)
UIView.animateWithDuration(0.2, animations: { () -> Void in
fromView.frame = CGRectMake(
scrollRight ? -screenWidth : screenWidth,
viewSize.origin.y,
screenWidth,
viewSize.size.height
)
toView.frame = CGRectMake(
0,
viewSize.origin.y,
screenWidth,
viewSize.size.height
)
}) { (finished) -> Void in
if(finished) {
fromView.removeFromSuperview()
tabBarController.selectedIndex = controllerIndex
}
}
return false;
}