您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發基礎

如何用程式碼控制以不同螢幕方向打開新頁面【iOS】

編輯:iOS 開發基礎

  • 程式碼範例:https://github.com/johnlui/Swift-On-iOS/tree/master/ControlOrientation/ControlOrientation

  • 環境要求:Xcode 7 / Swift 2.0

前兩天遇到一個「使用指定的不同螢幕方向打開新頁面」的需求。需求很簡單:App 一直保持豎屏,要求新打開的頁面能夠指定為橫屏或豎屏,並且不允許自動切換,新頁面退出後要恢復豎屏。

準備工作

新建一個單頁面專案,命名為 ControlOrientation。接著取消 Landscape Right 的勾選,我們的 App 將只支援豎屏(Portrait)與 Landscape Left:

以橫屏打開新頁面

給 Main.storyboard 拖入一個 View Controller,新建一個繼承自 UIViewController 的類 SecondViewController,然後把兩者綁定。之後給新建的這個 View Controller 賦予 StoryBoard ID 值「secondVC」:

簡單 Google 一下便能得到用程式碼指定橫屏打開新頁面的方式。在新頁面的 viewDidLoad 中執行:

UIDevice.currentDevice().setValue(UIInterfaceOrientation.LandscapeLeft.rawValue, forKey: "orientation")

在啟動頁中間加入一個居中的按鈕,拖動綁定點擊事件,加入載入新頁面的程式碼:

@IBAction func openNewVC(sender: AnyObject) {     if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("secondVC") as? SecondViewController {         self.presentViewController(vc, animated: true, completion: nil)     } }

運行!查看效果:

程式碼控制橫屏成功!

還有一個收尾工作:給 SecondViewController 加一個退出按鈕,這步就不再描述了。然後再次運行專案,查看效果:

出來之後也變橫屏了,這可不是我們想要的。

螢幕方向鎖定

重讀文章開頭的需求,我們就會發現:需求要求我們在任何一個介面都不能因為手機物理方向的變化而自動改變螢幕方向。稍微 Google 一下,我們就會得到鎖死螢幕方向的程式碼。給 ViewController 與 SecondViewController 都加一個函數:

override func shouldAutorotate() -> Bool {     return false }

為了直觀展示,我們將透過手動改變模擬器的方向來模擬真機螢幕方向的變化(改變模擬器螢幕方向的快捷鍵是 Command 加左或右),檢驗效果:

簡直完美!是不是覺得有點太簡單了?這麼容易就實現了?當然不是,這麼容易我還寫個什麼文章呀。

神級 BUG

既然新打開橫屏已經完美解決,那麼新打開豎屏呢?讓我們把那一行切換螢幕方向的程式碼註釋掉,運行查看結果:

BUG 了!!

解決 BUG

這個 bug 我搞了兩天,雖然不是一直在弄它,但總時長也至少有八個小時。嚴格來說,這並不是 bug,只是我們不知道怎麼搞定而已。最後我終於搞明白了這個 BUG 背後的運行原理,先說解決方案:

豎屏解決方案

在第一個頁面 ViewController 中加入以下兩個函數:

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {     return UIInterfaceOrientationMask.Portrait } override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {     return UIInterfaceOrientation.Portrait }

在第二個頁面 SecondViewController 中加入兩個函數:

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {     return UIInterfaceOrientationMask.Portrait } override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {     return UIInterfaceOrientation.Portrait }

查看效果:

豎屏搞定!

橫屏解決方案

將 SecondViewController 中的函數改為:

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {     return UIInterfaceOrientationMask.All } override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {     return UIInterfaceOrientation.LandscapeLeft }

別忘了打開 viewDidLoad 中的橫屏控制程式碼。查看效果:

橫屏搞定!

全搞定了嗎?並沒有

我們還是 Too Young Too Simple。在我們簡陋的例子中似乎確實全搞定了,但實際工程中大多數專案都不是普通 View Controller 作為根控制器。我們測試一下 QQ、微信、微博等 App 採用的通用方案:根控制器為 TabBarController,之後嵌套 NavigationController,然後放入 ViewController 頁面展示,再 present 出 SecondViewController。

搭 VIE TNV(TabBarController→NavigationController→ViewController)架構

我們可不是跟風在國外上市的中國互聯網公司拆 VIE 架構哦~

  1. 選中 ViewController,點擊菜單中的 Editor → Embed In → Navigation Controller,第一層嵌套完成。

  2. 選中 NavigationController,點擊菜單中的 Editor → Embed In → Tab Bar Controller,第二層嵌套完成。

如圖:

查看效果:

這什麼玩意兒 o(╯□╰)o

終極解決方案

我埋坑的過程就不細說了,下面直接給出特性分析與解決方案:

特性分析

跟 shouldAutorotate() 不同,判斷是否應該改變 App 螢幕方向並不會檢測當前顯示的 View Controller 的屬性,而是去檢測根 View Controller 的屬性,所以我們要從 TabBarController 一路取得當前 View Controller。

解決方案

新建一個繼承於 UITabBarController 的類 TabBarController,將其和 StoryBoard 中的 Tab Bar Controller 頁面綁定。該類完整程式碼如下:

import UIKit class TabBarController: UITabBarController, UITabBarControllerDelegate {     override func viewDidLoad() {         super.viewDidLoad()         self.delegate = self     }     override func didReceiveMemoryWarning() {         super.didReceiveMemoryWarning()     }          override func shouldAutorotate() -> Bool {         return false     }     func tabBarControllerSupportedInterfaceOrientations(tabBarController: UITabBarController) -> UIInterfaceOrientationMask {         return self.selectedViewController!.supportedInterfaceOrientations()     }     func tabBarControllerPreferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {         return self.selectedViewController!.preferredInterfaceOrientationForPresentation()     } }

同樣,新建一個繼承於 UINavigationController 的類 NavigationController,將其和 StoryBoard 中的 Navigation Controller 頁面綁定,向該類加入以下程式碼:

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {     return self.visibleViewController!.supportedInterfaceOrientations() } override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {     return self.visibleViewController!.preferredInterfaceOrientationForPresentation() }

檢查結果:

寫在最後

透過本文我們可以總結出以下兩點:

  1. 是否自動轉換螢幕方向由當前顯示的 View Controller 決定。

  2. 是否支援橫屏、是否優先選擇橫屏由 rootViewController 決定,若有多層結構嵌套,則需要層層傳遞,把控制權交給當前顯示的頁面,類似代理傳值與延遲靜態綁定。

另外,我利用 Swift 的 enum 給 SecondViewController 類加上了一個優雅的橫屏/豎屏控制開關,具體程式碼可以在 GitHub 上查看。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved