微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[Swift通天遁地]一、超级工具-(14)使用SweetAlert(github项目)制作漂亮的自定义Alert窗口

本文将演示一款非常强大的警告窗口。

Github地址:【SweetAlert】

下载所需的第三方类库。在下载的文件夹中,选择:

【SweetAlert】->【SweetAlert.swift】拖动到项目中

->在弹出的添加文件窗口中,保持认的设置选项->【Finish】

在项目导航区,打开视图控制器的代码文件【ViewController.swift】

现在编写代码,创建一个警告窗口窗口。

 1 import UIKit
 2 
 3 class ViewController: UIViewController {
 4     
 5     //添加一个警告窗口,作为当前类的一个属性
 6     var alert = SweetAlert()
 7     override func viewDidLoad() {
 8         super.viewDidLoad()
 9         // Do any additional setup after loading the view,typically from a nib.
10         
11         //初始化一个按钮,当用户点击该按钮时,弹出一个警告窗口
12         let popup = UIButton(frame: CGRect(x: 0,y: 0,width: 280,height: 40))
13         //将按钮控件放置在根视图的中心位置
14         popup.center = self.view.center
15         //设置按钮控件的背景颜色为橙色
16         popup.backgroundColor = UIColor.orange
17         //设置按钮控件在正常状态下的标题文字
18         popup.setTitle("SweetAlert",for: .normal)
19         //给按钮控件绑定点击事件
20         popup.addTarget(self,21                         action: #selector(ViewController.cancelAndConfirm(_:)),22                         for: .touchUpInside)
23         
24         //设置根视图的背景颜色为橙色
25         self.view.backgroundColor = UIColor.orange
26         //并将按钮添加到根视图中
27         self.view.addSubview(popup)
28     }
29     
30     //添加一个方法,用来响应按钮的点击事件
31     func aBasicmessageAlert(_ sender: AnyObject)
32     {
33         //当按钮被点击时,初始化并弹出一个警告窗口,同时设置窗口中的文字信息。
34         _ = SweetAlert().showAlert("Here‘s a message!")
35     }
36     
37     //代码进行一些修改
38     //添加一个方法,用来响应按钮的点击事件,记得修改按钮的方法绑定语句
39     func subtitleAlert(_ sender: AnyObject) {
40         //初始化并弹出一个警告窗口,同时设置窗口中的标题、子标题和样式
41         _ = SweetAlert().showAlert("Here‘s a message!",//标题
42                                    subTitle: "It‘s pretty,isn‘t it?",//标题
43                                    style: AlertStyle.none)//样式
44     }
45     
46     //代码进行一些修改
47     //添加一个方法,用来响应按钮的点击事件,记得修改按钮的方法绑定语句
48     func sucessAlert(_ sender: AnyObject)
49     {
50         //初始化并弹出一个警告窗口,同时设置窗口中的标题、子标题,并将窗口样式设置为成功样式。
51         _ = SweetAlert().showAlert("Good job!",52                                    subTitle: "You clicked the button!",53                                    style: AlertStyle.success)
54     }
55     
56     //代码进行一些修改
57     //添加一个方法,该方法用来创建带有多个按钮的弹出窗口,记得修改按钮的方法绑定语句
58     func cancelAndConfirm(_ sender: AnyObject)
59     {
60         //初始化并弹出一个警告窗口,同时设置窗口中的标题、子标题和样式。
61         //添加了两个不同外观样式的按钮。
62         _ = SweetAlert().showAlert("Are you sure?",63                                    subTitle: "You file will permanently delete!",64                                    style: AlertStyle.warning,65                                    buttonTitle:"No,cancel plx!",66                                    buttonColor:UIColor.colorFromrGB(0xD0D0D0),67                                    otherButtonTitle:  "Yes,delete it!",68                                    otherButtonColor: UIColor.colorFromrGB(0xDD6B55))
69          //添加一条语句,用来响应按钮被点击的事件。
70         { (isOtherButton) -> Void in
71             if isOtherButton == true
72             {
73                 //当第二个按钮被点击时,弹出另一个错误类型的警告窗口。
74                 _ = SweetAlert().showAlert("Cancelled!",subTitle: "Your imaginary file is safe",style: AlertStyle.error)
75             }
76             else
77             {
78                 //当第一个按钮被点击时,弹出一个成功类型的警告窗口。
79                 _ = SweetAlert().showAlert("Deleted!",subTitle: "Your imaginary file has been deleted!",style: AlertStyle.success)
80             }
81         }
82     }
83     
84     //添加一个方法,该方法用来创建拥有自定义图标的窗口,记得修改按钮的方法绑定语句。
85     func customIconAlert(_ sender: AnyObject)
86     {
87         //创建一个自定义图标样式的窗口,图标时项目中的一张图片
88         _ = SweetAlert().showAlert("Sweet!",89                                    subTitle: "Here‘s a custom image.",90                                    style: AlertStyle.customImag(imageFile: "coffee.png"))
91     }
92     
93     override func didReceiveMemoryWarning() {
94         super.didReceiveMemoryWarning()
95         // dispose of any resources that can be recreated.
96     }
97 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐