iOS開發中界面跳轉有兩種方式,上下跳轉和左右跳轉。
上下跳轉_TO:
- let secondViewController = SecondViewController()
- self.presentViewController(secondViewController, animated: true, completion: nil)
上下跳轉_BACK:
- dismissViewControllerAnimated(true, completion: nil)
-----------------------------------------------
-----------------------------------------------
左右跳轉_TO:
(將新的視圖控制器PUSH到navigationController中,相當於入棧操作)
- let secondViewController = SecondViewController()
- self.navigationController!.pushViewController(secondViewController, animated: true)
左右跳轉_BACK:
(將當前視圖控制器從導航視圖控制器堆棧中移除,從而返回到了上一級界面)
( - ) BACK_到上一級:
- let firstViewController = FirstViewController()
- self.navigationController?.popViewControllerAnimated(true)
( - ) BACK_指定界面:
- // 獲得視圖控制器中的某一視圖控制器
- let viewController = self.navigationController?.viewControllers[0]
- self.navigationController?.popToViewController(viewController as! UIViewController, animated: true)
( - ) BACK_根視圖:
- self.navigationController?.popToRootViewControllerAnimated(true)
根視圖的設置需要在AppDelegate中設置
- var window: UIWindow?
- func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
- {
- var firstViewController = FirstViewController()
- var rootNavigationViewController = UINavigationController(rootViewController: firstViewController)
- self.window!.rootViewController = rootNavigationViewController
- return true
- }
OC針對手寫頁面及storyboard制作頁面,使用代碼進行頁面跳轉的兩種方法。
-
-
▪ 手寫頁面:
var vc = ViewController() self.presentViewController(vc, animated: true, completion: nil)
▪ storyboard制作頁面
var sb = UIStoryboard(name: "Main", bundle:nil) var vc = sb.instantiateViewControllerWithIdentifier("VC") as ViewController //VC為該界面storyboardID,Main.storyboard中選中該界面View,Identifier inspector中修改 self.presentViewController(vc, animated: true, completion: nil)
self.performSegueWithIdentifier("VC", sender: nil)
多個場景之間切換的樣式(Style)總共有5個:
Modal(模態)
-- 過渡到另一個場景,以完成一項任務。任務完成后,將關閉該場景,並返回到原來的場景。
Push(壓入)
-- 創建一個場景鏈,用戶可在其中前后移動。用於導航視圖控制器。
Replace(替換,僅適用於iPad)
-- 替換當前場景,用於一些iPad特有的視圖控制器。
Popover(彈出框,僅適用於iPad) --
一個帶箭頭的彈出框。
Custome(自定義)
-- 通過編譯在場景之間進行自定義過渡。
過渡類型(Transition)是從一個場景切換到另一個場景時播放的動畫。有4個選項:
Cover Vertical
-- 新場景從下向上移動,逐漸覆蓋舊場景。
Flip Horizontal
-- 視圖水平翻轉,以顯示背面的新場景。
Cross Dissolve
-- 舊場景淡出,新場景淡入。
Partial Curl
-- 舊場景像書頁一樣翻開,顯示下面的新場景。
在iPad應用程序中,還會多出一個Presentation屬性,它決定了模態視圖在屏幕上的顯示方式。有4種顯示樣式:
Form Sheet(表單)
-- 將場景調整到比屏幕小(不管朝向),並在當前場景后面顯示原始場景,這幾乎相當於在一個iPad窗口中顯示。
Page Sheet(頁面)
-- 調整場景大小,使其以縱向格式顯示。Full
Screen(全屏)
-- 調整場景大小,使其覆蓋整個屏幕。
Current Context(當前上下文)
-- 以原始場景的顯示方式展示場景。
要使用在故事板中定義的切換到另一個場景,但又不想自動觸發該切換,可使用UIViewController的實例方法performSegueWithIdentifier:sender。調用該方法后,切換就將啟動並發生過渡。應將參數sender設置為啟動切換的對象。這樣在切換期間,就可確定是哪個對象啟動了切換。
- (IBAction)toConfigHandler:(id)sender { //執行名為"toConfig"的切換 [self performSegueWithIdentifier:@"toConfig" sender:self];}
調用UIViewController的方法dismissViewControllerAnimated:completion,可以關閉當前模態視圖,返回到原始場景。completion是一個可選參數,用於指定過渡完畢后將執行的代碼塊。
- (IBAction)returnToMainHandler:(id)sender { //關閉模態場景 [self dismissViewControllerAnimated:YES completion:nil];}
以純代碼的方式創建模態場景切換:
//獲取"MyMain.storyboard"故事板的引用UIStoryboard *mainStoryboard =[UIStoryboard storyboardWithName:@"MyMain" bundle:nil]; //實例化Identifier為"myConfig"的視圖控制器 ConfigViewController *configVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"myConfig"]; //為視圖控制器設置過渡類型 configVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical; //為視圖控制器設置顯示樣式 configVC.modalPresentationStyle = UIModalPresentationFullScreen; //顯示視圖 [self presentViewController:configVC animated:YES completion:nil];
視圖的modalTransitionStyle(過渡類型)屬性有以下枚舉值:
UIModalTransitionStyleCoverVertical
-- 默認值,從下向上覆蓋
UIModalTransitionStyleFlipHorizontal
-- 水平翻轉
UIModalTransitionStyleCrossDissolve
-- 淡入淡出
UIModalTransitionStylePartialCurl
-- 像書頁一樣翻開以顯示下面的視圖
視圖的modalPresentationStyle(顯示樣式)屬性有以下枚舉值:
UIModalPresentationFullScreen
-- 默認值,如何旋轉都是全屏,iPhone下僅有這一個樣式有效
UIModalPresentationFormSheet
-- 寬度和高度均會小於屏幕尺寸,居中顯示,四周是變暗區域。僅適用於
iPadUIModalPresentationPageSheet
-- 在豎屏下和UIModalPresentationFullScreen表現一樣,橫屏下高度和當前屏幕高度相同,寬度和豎屏模式下屏幕寬度相同,剩余未覆蓋區域將會變暗並阻止用戶點擊
UIModalPresentationCurrentContext
-- 與父視圖的顯示樣式相同
-