3, Swift 中彈窗的應用UIAlertController
阿新 • • 發佈:2018-12-31
在應用中有很多的彈窗坑定是需要的,這裡隨便寫了幾個系統的UIAlertController的簡單應用,主要有四個
1,常規彈窗,確定按鈕,取消按鈕,或者只有確定,或者取消按鈕;
2,選擇圖片的從底部滑動的彈窗;
3,帶輸入框的彈窗
4,一閃而過的彈框,但是這種樣式寫死,沒法改變背景顏色等等這些東西;
程式碼如下
import UIKit
class AlertController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let button = UIButton();
button.frame = CGRect.init(x: 20, y: 100, width: UIScreen.main.bounds.width - 2*20, height: 56);
button .titleLabel?.font = UIFont.systemFont(ofSize: 16);
button.setTitle("確定取消只展示訊息", for: UIControlState.normal)
button .setTitleColor(UIColor .red, for: UIControlState.normal);
button .backgroundColor = UIColor.orange;
button.addTarget(self, action: #selector(alertTypeNormal), for: UIControlEvents.touchUpInside)
self.view .addSubview(button);
let sheetBtn = UIButton();
sheetBtn.frame = CGRect.init(x: 20, y: 156+20 , width: UIScreen.main.bounds.width - 2*20, height: 56);
sheetBtn .titleLabel?.font = UIFont.systemFont(ofSize: 16);
sheetBtn.setTitle("底部滑出", for: UIControlState.normal)
sheetBtn .setTitleColor(UIColor .red, for: UIControlState.normal);
sheetBtn .backgroundColor = UIColor.gray;
sheetBtn.addTarget(self, action: #selector(alertSheet), for: UIControlEvents.touchUpInside)
self.view .addSubview(sheetBtn);
let logBtn = UIButton();
logBtn.frame = CGRect.init(x: 20, y: 156+20 + 76 , width: UIScreen.main.bounds.width - 2*20, height: 56);
logBtn .titleLabel?.font = UIFont.systemFont(ofSize: 16);
logBtn.setTitle("帶有textField控制元件的", for: UIControlState.normal)
logBtn .setTitleColor(UIColor .red, for: UIControlState.normal);
logBtn .backgroundColor = UIColor.green;
logBtn.addTarget(self, action: #selector(alertTF), for: UIControlEvents.touchUpInside)
self.view .addSubview(logBtn);
let disBtn = UIButton();
disBtn.frame = CGRect.init(x: 20, y: 156+20 + 76 + 76 , width: UIScreen.main.bounds.width - 2*20, height: 56);
disBtn .titleLabel?.font = UIFont.systemFont(ofSize: 16);
disBtn.setTitle("一閃而過的效果", for: UIControlState.normal)
disBtn .setTitleColor(UIColor .red, for: UIControlState.normal);
disBtn .backgroundColor = UIColor.brown;
disBtn.addTarget(self, action: #selector(alertDismiss), for: UIControlEvents.touchUpInside)
self.view .addSubview(disBtn);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/**一般彈窗型別 有確定和取消,可以只有一個按鈕的將action修改就可以了
destructive 使用此風格 字型顏色為紅色 其他型別位系統預設的藍色**/
func alertTypeNormal(){
let alertNormal = UIAlertController(title: "系統提示", message: "無上的神偉大的小明", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
let sureAction = UIAlertAction(title: "確定", style: .destructive) { (UIAlertAction) in
print("點選了確定")
}
alertNormal.addAction(cancelAction);
alertNormal.addAction(sureAction);
self.present(alertNormal, animated: true) {
}
}
/**一般用於選擇圖片或者拍照的彈框從底部滑出**/
func alertSheet(){
let alertsheet = UIAlertController(title: "請選擇圖片或拍攝圖片", message: "", preferredStyle: .actionSheet)
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: { (UIAlertAction) in
print("選擇了取消");
})
let photoAction = UIAlertAction(title: "相機", style: .default) { (UIAlertAction) in
print("選擇了相機");
}
let tumbAction = UIAlertAction(title: "相簿", style: .default) { (UIAlertAction) in
print("選擇了相簿");
}
alertsheet.addAction(cancelAction);
alertsheet.addAction(photoAction);
alertsheet.addAction(tumbAction);
self.present(alertsheet, animated: true) {
}
}
/**帶有輸入框的彈窗**/
func alertTF(){
let alertController = UIAlertController(title: "登入頁面",message: "輸入使用者名稱和密碼", preferredStyle: .alert)
alertController.addTextField {
(textField: UITextField!) -> Void in
textField.placeholder = "請輸入使用者名稱"
}
alertController.addTextField {
(textField: UITextField!) -> Void in
textField.placeholder = "請輸入密碼"
textField.isSecureTextEntry = true
}
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
let okAction = UIAlertAction(title: "登入", style: .default, handler: {
action in
//也可以用下標的形式獲取textField let login = alertController.textFields![0]
let login = alertController.textFields!.first!
let password = alertController.textFields!.last!
print("使用者名稱:\(login.text) 密碼:\(password.text)")
})
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
/**規定時間後消失的彈框**/
func alertDismiss(){
let alertController = UIAlertController(title: "儲存成功!",message: nil, preferredStyle: .alert)
//顯示提示框
self.present(alertController, animated: true, completion: nil)
//兩秒鐘後自動消失
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
self.presentedViewController?.dismiss(animated: false, completion: nil)
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}