讓 present modally 全螢幕顯示下一頁

從 iOS 13 開始,當我們用 present modally 顯示下個畫面時,它預設會以酷炫的卡片方式呈現,比方以下例子:

不過有些畫面我們還是希望能全螢幕,可以像從前一樣 full screen 嗎 ?

當然可以。

present modally 呈現新頁面的方式跟 modalPresentationStyle 有關,iOS 13 預設的 modalPresentationStyle 是 automatic,表示系統會自動幫我們選擇最適合的方式。根據以下官方文件的說明,automatic 大部分的時候會選擇以 pageSheet 的方式呈現。

For most view controllers, UIKit maps this style to the UIModalPresentationStyle.pageSheet style, but some system view controllers may map to a different style.

而從 pageSheet 的說明我們看到它會蓋住部分的之前畫面,因此在 iOS 13,它將讓 App 畫面以卡片的形式呈現。

A presentation style that partially covers the underlying content.

接下來就讓我們以 Roald Dahl 有趣的女巫為例說明吧。

拉 segue,選擇 Present Modally

點選 segue,觀察它的 Presentation 欄位

segue 的 Presentation 是 Same As Destination,而 witches 圖片頁面的 Presentation 是 Automatic,因此modalPresentationStyle 預設的確是 automatic。

那要怎樣才能 full screen 呢 ? 方法很多,以下我們介紹 3 個方法任君挑選。

方法1: 將 segue 的 Presentation 改成 Full Screen

方法2: 將圖片頁面 controller 的 Presentation 改成 Full Screen

方法3:從程式將圖片頁面 controller 的 modalPresentationStyle 設為 fullScreen

不透過 segue,從程式呼叫 present 切換頁面。

@IBAction func showWitches(_ sender: Any) {

if let witchesViewController = storyboard?.instantiateViewController(identifier: "\(WitchesViewController.self)") {
witchesViewController.modalPresentationStyle = .fullScreen
present(witchesViewController, animated: true)
}
}

--

--

彼得潘的 iOS App Neverland
彼得潘的 Swift iOS App 開發問題解答集

彼得潘的iOS App程式設計入門,文組生的iOS App程式設計入門講師,彼得潘的 Swift 程式設計入門,App程式設計入門作者,http://apppeterpan.strikingly.com