[Swift通天遁地]1、超級工具-(15)使用SCLAlertView製做強大的Alert警告窗口和Input編輯窗口

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-thpythqf-hu.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

目錄:[Swift]通天遁地Swiftios

本文將演示如何知錯一款能夠採集用戶數據的提示窗口。git

首先確保在項目中已經安裝了所需的第三方庫。github

1 platform :ios, '12.0'
2 use_frameworks!
3 
4 target 'DemoApp' do
5     source 'https://github.com/CocoaPods/Specs.git'
6     pod 'SCLAlertView'
7 end

點擊【Podfile】,查看安裝配置文件。swift

根據配置文件中的相關配置,安裝第三方庫微信

而後點擊打開【DemoApp.xcworkspace】項目文件。閉包

在項目導航區,打開視圖控制器的代碼文件【ViewController.swift】app

  1 import UIKit
  2 //在當前的項目文件中,引入已經安裝的第三方類庫
  3 import SCLAlertView
  4 
  5 class ViewController: UIViewController {
  6 
  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: 320, height: 40))
 13         //將按鈕控件放置在根視圖的中心位置
 14         popup.center = self.view.center
 15         //設置按鈕控件的背景顏色爲橙色
 16         popup.backgroundColor = UIColor.orange
 17         //設置按鈕控件在正常狀態下的標題文字
 18         popup.setTitle("Popup", for: .normal)
 19         //給按鈕控件綁定點擊事件
 20         popup.addTarget(self, action: #selector(ViewController.popUp), for: .touchUpInside)
 21         
 22         //設置根視圖的背景顏色爲橙色
 23         self.view.backgroundColor = UIColor.orange
 24         //並將按鈕添加到根視圖中
 25         self.view.addSubview(popup)
 26     }
 27 
 28     //添加一個方法,用來響應按鈕的點擊事件
 29     func popUp()
 30     {
 31         //1.初始化一個信息類型的彈出窗口,
 32         //並設置彈出窗口的標題和子標題。
 33         SCLAlertView().showInfo("Important info", subTitle: "You are great")
 34         
 35         //2.對代碼進行一些修改
 36         //初始化一個成功類型的彈出窗口,並設置彈出窗口的標題和子標題。
 37         let alertViewResponder: SCLAlertViewResponder = SCLAlertView().showSuccess("Hello World", 
 38                                                         subTitle: "This is a more descriptive text.")
 39         //能夠經過彈出窗口的設置標題方法,從新設置窗口的標題文字。
 40         alertViewResponder.setTitle("New Title")
 41         //能夠經過彈出窗口的設置子標題方法,從新設置窗口的子標題文字。
 42         alertViewResponder.setSubTitle("New description")*/
 43         
 44         //3.對代碼進行一些修改
 45         //彈出窗口包含多種樣式。
 46         //錯誤類型的彈出窗口,並設置彈出窗口的標題和子標題。
 47         SCLAlertView().showError("Hello Error", subTitle: "This is a more descriptive error text.")
 48         //通知類型的彈出窗口,並設置彈出窗口的標題和子標題。
 49         SCLAlertView().showNotice("Hello Notice", subTitle: "This is a more descriptive notice text.")
 50         //警告類型的彈出窗口,並設置彈出窗口的標題和子標題。
 51         SCLAlertView().showWarning("Hello Warning", subTitle: "This is a more descriptive warning text.")
 52         //信息類型的彈出窗口,並設置彈出窗口的標題和子標題。
 53         SCLAlertView().showInfo("Hello Info", subTitle: "This is a more descriptive info text.")
 54         //編輯類型的彈出窗口,並設置彈出窗口的標題和子標題。
 55         SCLAlertView().showEdit("Hello Edit", subTitle: "This is a more descriptive info text.")
 56         
 57 
 58         //4.對代碼進行一些修改
 59         //對彈出窗口的視覺樣式進行了修改
 60         SCLAlertView().showTitle(
 61             "Congratulations",//標題
 62             subTitle: "Operation successfully completed.",//子標題
 63             duration: 2.0,//持續時長
 64             completeText: "Done",//完成提示文字
 65             style: .success,//主題樣式
 66             colorStyle: 0xA429FF,//窗口顏色
 67             colorTextButton: 0xFFFFFF//按鈕文字的顏色
 68         )
 69 
 70         //5.對代碼進行一些修改
 71         //也能夠初始化一個外觀樣式類的對象,從而進行窗口樣式的設置
 72         let appearance = SCLAlertView.SCLAppearance(
 73             //標題字體
 74             kTitleFont: UIFont(name: "HelveticaNeue", size: 20)!,
 75             //內容文字字體
 76             kTextFont: UIFont(name: "HelveticaNeue", size: 14)!,
 77             //按鈕字體
 78             kButtonFont: UIFont(name: "HelveticaNeue-Bold", size: 14)!,
 79             //設置不容許關閉按鈕的顯示
 80             showCloseButton: false
 81         )
 82         
 83         //根據上文的外觀樣式對象,初始化一個指定外觀樣式的彈出窗口。
 84         let alert = SCLAlertView(appearance: appearance)
 85         //調用窗口顯示的通知方法,建立一個通知類型的彈出窗口。
 86         alert.showNotice("Hello Notice", subTitle: "This is a more descriptive notice text.")
 87 
 88         //6.對代碼進行一些修改
 89         //初始化一個警告窗口
 90         let alertView = SCLAlertView()
 91         //在窗口中添加第一個按鈕控件,並給按鈕綁定點擊事件
 92         alertView.addButton("First Button", target:self, selector:Selector("firstButton"))
 93          //在窗口中添加第二個按鈕控件,並給按鈕添加一個閉包語句,以響應按鈕的點擊事件
 94         alertView.addButton("Second Button")
 95         {
 96             print("Second button tapped")
 97         }
 98         //調用窗口的顯示成功方法,建立一個成功類型的彈出窗口
 99         alertView.showSuccess("Button View", subTitle: "This alert view has buttons")
100 
101         //7.對代碼進行一些修改,建立一個自動隱藏的彈出窗口
102         //初始化一個外觀樣式常量
103         let appearance = SCLAlertView.SCLAppearance(
104             //並設置在窗口中不顯示關閉按鈕
105             showCloseButton: false
106         )
107         //根據外觀樣式,建立一個彈出窗口對象
108         let alertView = SCLAlertView(appearance: appearance)
109         //調用窗口的顯示警告方法,打開一個警告類型的彈出窗口,並設置在顯示三秒以後自動關閉彈出窗口
110         alertView.showWarning("No button", 
111                               subTitle: "Just wait for 3 seconds and I will disappear",
112                               duration: 3)
113         //初始化一個外觀樣式常量
114         let appearance = SCLAlertView.SCLAppearance(
115             //設置在窗口中顯示圓形圖標
116             showCircularIcon: true
117         )
118         //根據外觀樣式,建立一個彈出窗口對象
119         let alertView = SCLAlertView(appearance: appearance)
120         //從項目中讀取一張圖片素材
121         let alertViewIcon = UIImage(named: "Hearts")
122         //調用窗口的顯示信息方法,打開一個信息類型的彈出窗口,並設置窗口頂部的圓形圖標
123         alertView.showInfo("Custom icon",
124                               subTitle: "This is a nice alert with a custom icon you choose",
125                               circleIconImage: alertViewIcon)
126 
127         //8.對代碼進行一些修改,建立一個文本輸入框的彈出窗口  
128         //初始化一個新的彈出窗口對象
129         let alert = SCLAlertView()
130         //往彈出窗口中,添加一個文本框,並設置文本框的佔位文字
131         let txt = alert.addTextField("Enter your name")
132         //往彈出窗口中添加一個按鈕,後跟一個閉包語句。
133         alert.addButton("Show Name") {
134             //當點擊該按鈕時,在控制檯輸出用戶在文本框中輸入的內容。
135             print("Text value: \(txt.text)")
136         }
137         //調用窗口的編輯方法,打開一個編輯類型的彈出窗口
138         alert.showEdit("Edit View", subTitle: "This alert view shows a text box")
139         
140         //9.對代碼進行一些修改,建立一個包含自定義視圖的彈出窗口
141         //初始化一個新的彈出窗口對象
142         let alert = SCLAlertView()
143         
144         //建立一個指定顯示區域的普通視圖
145         let subview = UIView(frame: CGRect(x: 0, y: 0, width: 216, height: 70))
146         //而後根據視圖的寬度,計算即將添加的文本框的水平位置
147         let x = (subview.frame.width - 180) / 2
148         
149         //初始化一個文本框,並將文本框放置在視圖的中心位置
150         let textfield1 = UITextField(frame: CGRect(x: x, y: 10, width: 180, height: 25))
151         //設置文本框邊緣的顏色
152         textfield1.layer.borderColor = UIColor.green.cgColor
153         //設置文本框邊緣的寬度
154         textfield1.layer.borderWidth = 1.5
155         //設置文本框的圓角半徑
156         textfield1.layer.cornerRadius = 5
157         //設置文本框的佔位文字
158         textfield1.placeholder = "Username"
159         //設置文本框的文字對齊方式
160         textfield1.textAlignment = NSTextAlignment.center
161         //將文本框添加到視圖中
162         subview.addSubview(textfield1)
163         
164         //使用相同的方式建立第二個文本框
165         let textfield2 = UITextField(frame: CGRect(x: x, y: textfield1.frame.maxY, width: 180, height: 25))
166         //設置文本框是否爲密文的顯示方式
167         textfield2.isSecureTextEntry = true
168         //設置文本框邊緣的顏色
169         textfield2.layer.borderColor = UIColor.blue.cgColor
170         //設置文本框邊緣的寬度
171         textfield2.layer.borderWidth = 1.5
172         //設置文本框的圓角半徑
173         textfield2.layer.cornerRadius = 5
174         //設置文本框的佔位文字
175         textfield2.placeholder = "Password"
176         //設置文本框的文字對齊方式
177         textfield2.textAlignment = NSTextAlignment.center
178         //將文本框添加到視圖中
179         subview.addSubview(textfield2)
180         
181         //設置彈出窗口的自定義子視圖屬性
182         alert.customSubview = subview
183         //給彈出窗口設置一個交互按鈕
184         alert.addButton("Login")
185         {
186             //並設置按鈕被點擊後的動做
187             print("Logged in")
188         }
189         
190         //添加一個交互按鈕
191         alert.addButton("Duration Button", //文本內容
192                         backgroundColor: UIColor.brown, //背景顏色
193                         textColor: UIColor.yellow, //文字顏色
194                         showDurationStatus: true)//按鈕的計時狀態屬性爲真,時間到後自動關閉窗口
195         {
196             print("Duration Button tapped")
197         }
198         
199         //調用窗口的顯示信息方法,打開一個信息類型的彈出窗口
200         alert.showInfo("Login", subTitle: "", duration: 5)
201     }
202     
203     override func didReceiveMemoryWarning() {
204         super.didReceiveMemoryWarning()
205         // Dispose of any resources that can be recreated.
206     }
207 }
相關文章
相關標籤/搜索