Swift-提示框(UIAlertController)使用
樣式一:
![2685270-9609a5bb59c6094b.png](https://i.iter01.com/images/3ba7574eb69c3a8d769fe208b757f6c4264854a2b486deade59c081aefc60c92.png)
image.png
let alertController = UIAlertController(title: "修改暱稱", message: nil, preferredStyle: UIAlertControllerStyle.alert)
alertController.addTextField(configurationHandler: { (textField: UITextField!) -> Void in
textField.placeholder = "請輸入暱稱"
// 新增監聽程式碼,監聽文字框變化時要做的操作
NotificationCenter.default.addObserver(self, selector: #selector(self.alertTextFieldDidChange), name: NSNotification.Name.UITextFieldTextDidChange, object: textField)
})
let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel, handler: nil)
let okAction = UIAlertAction(title: "確認", style: UIAlertActionStyle.default , handler: { (action: UIAlertAction!) -> Void in
let login = (alertController.textFields?.first)! as UITextField
let str = login.text
HWPrint("\(String(describing: str))")
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UITextFieldTextDidChange, object: nil)
})
okAction.isEnabled = false
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
/// 監聽文字改變
@objc func alertTextFieldDidChange(){
let alertController = self.presentedViewController as! UIAlertController?
if (alertController != nil) {
let login = (alertController!.textFields?.first)! as UITextField
let okAction = alertController!.actions.last! as UIAlertAction
if (!(login.text?.isEmpty)!) {
okAction.isEnabled = true
} else {
okAction.isEnabled = false
}
}
}
樣式二
![2685270-3a2cffd9957e6dcf.png](https://i.iter01.com/images/f70c379419a2b39d13965f136123053701ccac54b64117fd38c043b00299da34.png)
image.png
weak var weakSelf = self // 弱引用
let alertController = UIAlertController()
alertController.view.tintColor = UIColor.HWColorWithHexString(hex: "2E2E2E", alpha: 1) // 修改顏色
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
let directMessagesAction = UIAlertAction(title: "私信", style: .default) { (action) in
}
let focusOnAction = UIAlertAction(title: "關注", style: .default) { (action) in
}
alertController.addAction(focusOnAction)
alertController.addAction(directMessagesAction)
alertController.addAction(cancelAction)
weakSelf!.present(alertController, animated: true, completion: nil)
樣式三(單獨修改顏色 注:修改標題與內容失敗待研究)
![2685270-7a746c0b4c6a90a2.png](https://i.iter01.com/images/f862c5d99a7783e998f6788f36db79c823b2fdbab69daea97d771518c431fbcb.png)
image.png
func show() {
let actionSheet = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet)
//修改title字型及顏色
let title = "標題"
let titleStr = NSMutableAttributedString.init(string: title)
titleStr.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.orange, range: NSRange.init(location: 0, length: title.count))
titleStr.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize:20), range: NSRange.init(location: 0, length: title.count))
actionSheet.setValue(titleStr, forKey: "attributedTitle")
// 修改message字型及顏色
let message = "此處展示提示訊息"
let messageStr = NSMutableAttributedString.init(string: message)
messageStr.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.orange, range: NSRange.init(location: 0, length: message.count))
messageStr.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize:20), range: NSRange.init(location: 0, length: message.count))
actionSheet.setValue(messageStr, forKey: "attributedMessage")
let cancelAction = UIAlertAction.init(title: "取消", style: .cancel) { (action) in
}
// titleTextAlignment 位置
// titleTextColor 顏色
cancelAction.setValue("808080".hexColor(), forKey: "titleTextColor")
let action = UIAlertAction.init(title: "投訴", style: .default) { (action) in
}
action.setValue("333333".hexColor(), forKey: "titleTextColor")
actionSheet.addAction(cancelAction)
actionSheet.addAction(action)
UIApplication.shared.keyWindow?.rootViewController?.present(actionSheet, animated: true, completion: { })
}
相關文章
- Swift-方法Swift
- Swift-列舉Swift
- Swift-屬性Swift
- Swift-下標Swift
- Swift-繼承Swift繼承
- bootStrap4 提示框(tooltip)的使用boot
- Swift-函式(Functions)Swift函式Function
- vue-directive-tooltip 提示框的簡單使用Vue
- tooltip提示框
- Swift-控制流(Control Flow)Swift
- Swift-構造過程Swift
- Swift-析構過程Swift
- Swift-類和結構體Swift結構體
- jquery搜尋下拉提示框/登陸賬號下拉提示框jQuery
- Swift-語音識別、翻譯Swift
- Swift-逃逸閉包、自動閉包Swift
- PyQt5 之提示框QT
- 微信小程式提示框微信小程式
- 使用css的attr()寫一個類似a標籤title的提示框CSS
- 確定和取消提示框效果
- Swift-定位,編碼/反編碼功能的封裝Swift封裝
- Swift-方法排程-類的普通方法底層探究Swift
- 在echarts中自定義提示框內容Echarts
- win10 delate沒有提示框如何解決_win10 delate沒有提示框怎麼處理Win10
- Swift-為什麼實現Equatable不需要處理OptionalSwift
- Vue ElementUI 如何修改訊息提示框樣式---messageBoxVueUI
- html + css + js 原生 彈出提示框的實現HTMLCSSJS
- 重繪DevExpress的XtraMessageBox訊息提示框控制元件devExpress控制元件
- 點選刪除按鈕彈出是否刪除提示框
- Element 利用Tooltip提示框實現動態顯示文字提示
- Myeclipse的預設工作區間怎麼恢復提示框?Eclipse
- 短視訊直播原始碼,React children元件例項提示框原始碼React元件
- 手機直播原始碼,關於pyqt5彈出提示框原始碼QT
- 百度地圖開發-繪製點線提示框 07地圖
- oracle之 安裝oracle指定jdk 或者如何解決提示框顯示不全OracleJDK
- 設定代理後開啟應用老是彈出提示框要輸入使用者名稱和密碼的問題。密碼
- Win10電腦插入耳機後自動彈出提示框如何設定Win10
- 耳機插到win10電腦裡不自動彈出提示框如何解決Win10
- layui 樹形結構刪除沒有確認,原始碼加入confirm確認提示框UI原始碼