2015-09-24 37 views
8

定製化我有嵌入UINavigationController時與iOS 7/8內建但是當針對iOS的SDK 9建提出了一個錯誤的佈局,做工精細視圖控制器之間的自定義推送過渡。與NavigationController推動畫在iOS 9

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext 
{ 
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; 
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; 

    [transitionContext.containerView addSubview:toViewController.view]; 

    ... 

然後繼續執行動畫。問題是toViewController的內容即使在使用右上方佈局指南自動佈局約束時,也會顯示導航欄後面的內容。

儘管如此,它在iOS 8上運行良好,如果我們強制重繪(例如,將應用程序發送到背景並將其返回,將模式置於頂部並將其解除,等等)會導致整個自動佈局系統重繪自己並toViewController的視圖跳轉到正確的位置(作爲頂部佈局指南,來自導航欄的x像素而不是來自設備屏幕頂部的x像素)。

添加

[self.view setNeedsUpdateConstraints]; 
[self.view layoutIfNeeded]; 

作品如果放在viewDidAppear:animated,但viewDidLoadviewWillAppear:animated不起作用。這不是因爲用戶會看到當重繪在viewDidAppear:animated

回答

22

發生的事情我已經成功地解決我的問題的看法跳躍通過添加以下行addSubview:之前的解決方案:

toViewController.view.frame = [transitionContext finalFrameForViewController:toViewController]; 

從蘋果的文檔finalFrameForViewController:

返回指定視圖控制器的 視圖的結束幀矩形。

通過此方法返回的矩形表示在過渡結束時的對應視圖的大小 。用於在演示過程中被覆蓋的視圖 ,通過該 方法返回的值可以是CGRectZero但它也可能是一個有效的幀 矩形。

+0

救命恩人,感謝隊友 – beebcon

+0

你剛剛爲我節省了很多時間。非常感謝! –