Swift - 介面的跳轉模式

來源:互聯網
上載者:User

標籤:使用者   elf   present   toc   ble   boa   return   options   finish   

 

iOS開發中介面跳轉有兩種方式,上下跳轉和左右跳轉。

上下跳轉_TO:

 
  1. let secondViewController = SecondViewController()  
  2. self.presentViewController(secondViewController, animated: true, completion: nil)  

 

上下跳轉_BACK:

  1. dismissViewControllerAnimated(true, completion: nil)  


-----------------------------------------------

 

-----------------------------------------------

左右跳轉_TO:

(將新的視圖控制器PUSH到navigationController中,相當於入棧操作)

 
  1. let secondViewController = SecondViewController()  
  2. self.navigationController!.pushViewController(secondViewController, animated: true)  


左右跳轉_BACK:

 

(將當前視圖控制器從導航視圖控制器堆棧中移除,從而返回到了上一級介面)

( - ) BACK_到上一級:

 
  1. let firstViewController = FirstViewController()  
  2. self.navigationController?.popViewControllerAnimated(true)  


( - ) BACK_指定介面:

 

 
  1. // 獲得視圖控制器中的某一視圖控制器  
  2. let viewController = self.navigationController?.viewControllers[0]  
  3. self.navigationController?.popToViewController(viewController as! UIViewController, animated: true)  


( - ) BACK_根視圖:

 

 
  1. self.navigationController?.popToRootViewControllerAnimated(true)  

根視圖的設定需要在AppDelegate中設定 

 
  1. var window: UIWindow?  
  2.   
  3.   
  4. func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool  
  5. {  
  6.     var firstViewController = FirstViewController()  
  7.     var rootNavigationViewController = UINavigationController(rootViewController: firstViewController)  
  8.           
  9.     self.window!.rootViewController = rootNavigationViewController  
  10.           
  11.     return true  
  12.  

 

 

 

OC針對手寫頁面及storyboard製作頁面,使用代碼進行頁面跳轉的兩種方法。

    1. ? 手寫頁面:

      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

      -- 與父視圖的顯示樣式相同

       

Swift - 介面的跳轉模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.