單獨(dú)使用 addSubview 去加viewController的view發(fā)現(xiàn)有許多問(wèn)題,主要是使用了代理方法的。查了下發(fā)現(xiàn)問(wèn)題所在。 蘋果新的API增加了addChildViewController方法,并且希望我們?cè)谑褂胊ddSubview時(shí),同時(shí)調(diào)用[self addChildViewController:child]方法將sub view對(duì)應(yīng)的viewController也加到當(dāng)前ViewController的管理中。 對(duì)于那些當(dāng)前暫時(shí)不需要顯示的subview,只通過(guò)addChildViewController把subViewController加進(jìn)去;需要顯示時(shí)再調(diào)用transitionFromViewController方法。將其添加進(jìn)入底層的ViewController中。
//點(diǎn)擊切換頁(yè)面 - (void)didClick:(UIButton *)button { // 點(diǎn)擊處于當(dāng)前頁(yè)面的按鈕,直接跳出 其他頁(yè)面 [self replaceController:self.currentVC newController:self.firstVC];
}
// 切換各個(gè)標(biāo)簽內(nèi)容 - (void)replaceController:(UIViewController )oldController newController:(UIViewController )newController { /** * 著重介紹一下它 * transitionFromViewController:toViewController:duration:options:animations:completion: * fromViewController 當(dāng)前顯示在父視圖控制器中的子視圖控制器 * toViewController 將要顯示的姿勢(shì)圖控制器 * duration 動(dòng)畫時(shí)間(這個(gè)屬性,old friend 了 O(∩_∩)O) * options 動(dòng)畫效果(漸變,從下往上等等,具體查看API) * animations 轉(zhuǎn)換過(guò)程中得動(dòng)畫 * completion 轉(zhuǎn)換完成 */
[self addChildViewController:newController]; [self transitionFromViewController:oldController toViewController:newController duration:2.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) { if (finished) { //移除oldController,但在removeFromParentViewController:方法前不會(huì)調(diào)用willMoveToParentViewController:nil 方法,所以需要顯示調(diào)用 [newController didMoveToParentViewController:self]; [oldController willMoveToParentViewController:nil]; [oldController removeFromParentViewController]; self.currentVC = newController; }else{ self.currentVC = oldController; } }];}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注