#20 swift 以 ViewController 自訂 alertView 樣式

雖然 swift 以擁有 alert 樣式,只要呼叫就可以顯示。


我們需要兩個 ViewController 去製作這個畫面

一個是原始的 ViewController

另一個則作為 alertView 使用


我們可以在畫面開始的 ViewController 呼叫 ViewController

當作alert的 ViewController 他的 storyboard ID 設為 alert

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myAlert = storyboard.instantiateViewController(withIdentifier: "alert")
myAlert.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
myAlert.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
self.present(myAlert, animated: true, completion: nil)

然後在alert 頁面便可以自由的放置任何物件
透過背景透明度調整,可以呈現模仿 UIAlert的效果。

但還要加入 Alert 消失的效果

才能真正成為 Alert!

故在 alert 頁面UIButton加入下段

self.dismiss(animated: true, completion: nil)





Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Stephen Huang

Stephen Huang

Wish me luck on the way become iOS app developer!

More from Medium

How to build iOS Authentication with Firebase Part II Sign Up User 2022

Check UITableViewCell currently exists before tableview reloadRows刷新Cell前檢查Cell當下元件是否已生成

How to Add Amazing Frame-by-frame Animations in SwiftUI

Recent Advances in Differentiable Swift