透過 present 顯示 iOS SDK 內建的 controller

Present, Show

--

成品:翔太出歌必有保證,這首超好聽 !!! Aimer 音色真很特殊

前置作業:因為要用 present,Storyboard 很單純只有 lael & button

加入函式庫:想出現網頁和播影片,所以要加這 2 個

import SafariServices
import AVKit

顯示網頁 Controller

▲網頁 Controller 是SFSafariViewController
▲ present 設定顯示什麼,有無轉場動畫 / 執行任務

@IBAction func connectToWeb(_ sender: Any) {
if let url = URL(string: “https://www.shimizushota.com") {
let controller = SFSafariViewController(url: url) present(controller, animated: true, completion: nil)
}
}

顯示影片 Controller

▲影片 Controller 是 AVPlayerViewController
▲是影片網址不是網頁網址喔! 第 1 次我很天真地貼了 youtube 網址 😂
▲present 的 completion 本來會顯示 (() -> Void)?),按 Enter 會變大括號
➞此設定自動播放影片

@IBAction func playVideo(_ sender: Any) {
if let url = URL(string: “https://bit.ly/2U6EoIl"){
let player = AVPlayer(url: url)
let controller = AVPlayerViewController()
controller.player = player present(controller, animated: true) {
player.play()
}
}
}

不用 present 選用 show 也可以

▲ present 是 present modally 的 segue 效果,show 是 show 的 segue 效果
▲ 沒 navigation controller 就都會是 present modally 效果

show(controller, sender: nil)

其餘更多顯示 iOS SDK 內建的 controller請參考彼得教學

--

--