样式一:
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
}
}
}
样式二
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)
样式三(单独修改颜色 注:修改标题与内容失败待研究)
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: { })
}