一、模態(tài)視圖
視圖切換,純代碼的情況下,沒有NavigationController,一般會用到presentViewController來切換視圖并攜帶切換時的動畫。
其中切換方法如下:
– presentViewController:animated:completion: 彈出,出現(xiàn)一個新視圖 可以帶動畫效果,完成后可以做相應(yīng)的執(zhí)行函數(shù)經(jīng)常為nil
– dismissViewControllerAnimated:completion:退出一個新視圖 可以帶動畫效果,完成后可以做相應(yīng)的執(zhí)行函數(shù)經(jīng)常為nil
切換動畫在壓入一個新視圖和彈出頂層視圖均可以使用。
利用模態(tài)視圖進行多個頁面跳轉(zhuǎn)后,要返回最初始的頁面則需要了解到控制器的兩個屬性presentedViewController和presentingViewController,他們分別是被present的控制器和正在presenting的控制器。比如說, 控制器A和B,[A presentViewController B animated:YES completion:nil]; 那么A相對于B就是presentingViewController,B相對于A是presentedViewController,即這個時候
B.presentingViewController = A;
A.presentedViewController = B;
利用模態(tài)跳轉(zhuǎn),從A present到B,再從B present到C,然后從C present到D,最后要從D返回到A,返回過程如下:
-(void)dismissModalStack {
UIViewController *vc = self.presentingViewController;
while (vc.presentingViewController) {
vc = vc.presentingViewController;
}
[vc dismissViewControllerAnimated:YES completion:NULL];
}
presentingViewController返回的是除model外最頂層的ViewController,而不是彈出model的。
二、導(dǎo)航控制器UINavigationController
UINaviGationController通常被我們稱為導(dǎo)航欄,他是視圖與視圖之間聯(lián)系溝通的橋梁,切換方法如下:
推出某個視圖控制器
[self.navigationController pushViewController:viewController animated:YES];
UINavigationController是一個視圖控制器的容器,他里面可能放了很多個控制器,所以返回的時候可以分為幾種情況。
1、彈出當(dāng)前顯示的界面,返回到上個界面(注意,當(dāng)當(dāng)前界面是根界面時,這個方法是不起作用的)
[self.navigationController popViewControllerAnimated:YES];
2、返回到根視圖控制器
[self.navigationController popToRootViewControllerAnimated:YES];
3、彈出到指定視圖控制器
UIViewController *viewController=nil;
for (UIViewController *tempVc in self.navigationController.viewControllers) {
if ([tempVc isKindOfClass:[UIViewController class]]) {
viewController=tempVc;
}
}
[self.navigationController popToViewController:viewController animated:YES];
三、選項卡UITabBarController控制器
其實與其說UITabBarController的界面跳轉(zhuǎn),不如說是界面切換,因為UITabBarController的界面跳轉(zhuǎn)其實就是UITabBarController的viewControllers數(shù)組中的幾個界面切換。通過調(diào)用UITabBarController的addChildViewController方法添加子控制器:
UITabBarController *tabbarVC = [[ UITabBarController alloc ] init ];
OneViewController *oneVC = [[OneViewController ] init ];
oneVC.tabBarItem.title = @"one" ;
oneVC.tabBarItem.image = [ UIImage imageNamed : @"one.png" ];
TwoViewController *twoVC = [[TwoViewController ] init ];
twoVC.tabBarItem.title = @"two" ;
twoVC. tabBarItem.image = [UIImage imageNamed : @"two.png" ];
// 添加子控制器(這些子控制器會自動添加到UITabBarController的 viewControllers 數(shù)組中)
[tabbarVC addChildViewController :oneVC];
[tabbarVC addChildViewController :twoVC];
四、Storyboard的segues方式跳轉(zhuǎn)
此方法僅適用于Storyboard中各個頁面連線后的跳轉(zhuǎn),鼠標(biāo)點擊viewControlller,按住control鍵拖拽到另一個View頁面,在彈出的segue頁面中選擇跳轉(zhuǎn)模式即可,連線完之后選中連線,在Identifier填上對應(yīng)的標(biāo)示,然后再在需要跳轉(zhuǎn)的地方實現(xiàn)如下代碼即可:
[self performSegueWithIdentifier:@"test" sender:self];
如果連線的方式是push,則ViewController需要由UINavigationController來管理,返回方式則和UINavigationController一樣
如果連線的方式是model,則ViewController不需要由UINavigationController來管理,返回方式和模態(tài)的返回方式一樣
如果連線的方式是custom,則需要自定義segue,自定義segue在此不討論。
1.導(dǎo)航欄的基本屬性設(shè)置
導(dǎo)航欄的主要的子控件如下,其中都包含了所有的屬性和方法:
剩下的就是自定義左右barButtonItem,這個不就不多說了.
2.透明導(dǎo)航欄
1.思路是遍歷NavigationBar的子視圖,找到bar的背景圖片,控制它的hidden
4.導(dǎo)航欄隱藏問題
進入新的界面時隱藏狀態(tài)欄,view頂頭,在退出VC時記得把隱藏狀態(tài)改成NO