樣式一:
let alertController = UIAlertController(title: "修改昵稱", message: nil, preferredStyle: UIAlertControllerStyle.alert)
alertController.addTextField(configurationHandler: { (textField: UITextField!) -> Void in
textField.placeholder = "請(qǐng)輸入昵稱"
// 添加監(jiān)聽代碼,監(jiān)聽文本框變化時(shí)要做的操作
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: "確認(rèn)", 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)
}
/// 監(jiān)聽文字改變
@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
}
}
}
樣式二
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: "關(guān)注", style: .default) { (action) in
}
alertController.addAction(focusOnAction)
alertController.addAction(directMessagesAction)
alertController.addAction(cancelAction)
weakSelf!.present(alertController, animated: true, completion: nil)
樣式三(單獨(dú)修改顏色 注:修改標(biāo)題與內(nèi)容失敗待研究)
func show() {
let actionSheet = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet)
//修改title字體及顏色
let title = "標(biāo)題"
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: { })
}