- 2
-
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Test Btn:
let popup = UIButton(frame: CGRect(x: 0, y: 0, width: 280, height: 40))
popup.center = self.view.center
popup.titleLabel?.font = .systemFont(ofSize: 24)
popup.backgroundColor = UIColor.orange
popup.setTitle("Show sweet alert", for: .normal)
popup.addTarget(self, action: #selector(showSweetAlert(_:)), for: .touchUpInside)
self.view.backgroundColor = UIColor.orange
self.view.addSubview(popup)
}
@objc func showSweetAlert(_ sender: AnyObject){
yeTye()
}
// 弹窗:
func yeTye(){
//1.
_ = SweetAlert().showAlert("Here's a message!")
// 2. 设置样式 style: AlertStyle.none
//_ = SweetAlert().showAlert("Here's a message!", subTitle: "It's pretty, isn't it?", style: AlertStyle.none)
//3.
//_ = SweetAlert().showAlert("Good job!", subTitle: "You clicked the button!", style: AlertStyle.success)
}
}
-2
// 2. 自定义 弹窗:
func yeTye2(){
// 1. 带多个Btn 得弹窗:
let title = "Are you sure?"
let subTitle = "Your file will be permanently delete!"
let alertStyle = AlertStyle.warning // 样式;
// 两个Btn:
let buttonTitle = "Cancel"
let buttonColor = UIColor.lightGray
let otherButtonnTitle = "Yes, delete it!"
let otherButtonColor = UIColor.colorFromRGB(0xDD6B55)
// 初始化 弹窗: 设置相应的属性:
SweetAlert().showAlert(title,
subTitle: subTitle,
style: alertStyle,
buttonTitle: buttonTitle,
buttonColor: buttonColor,
otherButtonTitle: otherButtonnTitle,
otherButtonColor: otherButtonColor)
{ (isOtherButton) -> Void in
if isOtherButton == true { // 点击了 第二个Btn:
// 新的弹窗:
_ = SweetAlert().showAlert("Cancelled!", subTitle: "Your imaginary file is safe", style: AlertStyle.error)
} else {
// 点击了第一个Btn:
// 新的弹窗:
_ = SweetAlert().showAlert("Deleted!", subTitle: "Your imaginary file has been deleted!", style: AlertStyle.success)
}
}
}
// 3. 自定义图片 弹窗:
func yeTye3(){
_ = SweetAlert().showAlert("Sweet!",
subTitle: "Here's a custom image.",
style: .customImage(imageFile: "coffee.png"))
// coffee.png : 项目 本地的图片;
}