测试经过测试 | ✗ |
Lang语言 | SwiftSwift |
许可证 | MIT |
发布上次发布 | 2016年12月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 forgot 维护。
从苹果的文档中...
**继承** UIAlertController 类旨在直接使用,不支持继承。此类的视图层次结构是私有的,不得修改。
此外,还确定了并修复了一些意外的行为。例如,你是否知道你可以创建一个标题、信息,但没有操作的警告?虽然你将仍然得到一个警告,但没有办法将它删除,这将迫使你的用户退出你的应用程序。使用 FAAlertController,配置没有操作的通知或动作表单将自动允许用户通过在警告外部点击来删除它。
FAAlertController 与 UIAlertController
API 相似,所以只需使用 FAAlertController
和 FAAlertAction
。
// Create an alert
let alert = FAAlertController(title: "Some Title", message: "Your Message Here", preferredStyle: .alert)
// Create an action. All system styles (default, cancel, destructive) are supported.
let awesome = FAAlertAction(title: "Awesome", style: .default, handler: { (action) in
print("Doing something awesome....")
})
// Add actions and present the alert
alert.addAction(awesome)
present(alert, animated: true, completion: nil)
要使用深色版本,只需在初始化器中更改外观样式
let alert = FAAlertController(title: "A Dark Alert", message: "This isn't as foreboding as it sounds.", preferredStyle: .alert, appearance: .dark)
还包含对文本字段的兼容性
alert.addTextField { (textfield) in
textfield.placeholder = "Type Something Here!"
}
“选择器”样式警告需要一个符合 Pickable
的项目数组,并且通过一个符合 FAAlertControllerDelegate
的代理来处理选择。
func createPicker() {
let items = [itemOne, itemTwo, itemThree, itemFour, itemFive]
let picker = FAAlertController(title: "This Is A Picker", message: "Pick one of the options below!", preferredStyle: .picker, items: items)
picker.delegate = self
let cancel = FAAlertAction(title: "Cancel", style: .cancel)
picker.addAction(cancel)
present(picker, animated: true, completion: nil)
}
func didSelectItem(_ item: Pickable) {
print("Selected \(item)")
}
要自定义警告的外观,创建一个采用 FAAlertControllerAppearanceDelegate
的类型。通过分配给 FAAlertController.globalAppearanceDelegate
或单个警告的 appearanceDelegate
来自定义所有警告。分配给 appearanceDelegate
的类型会覆盖 globalAppearanceDelegate
,并且协议包含所有属性的默认实现,适用于两种外观样式。只需调整你需要的,然后继续,或者深入挖掘,使它成为你自己的。
class ViewController: UIViewController, FAAlertControllerAppearanceDelegate {
let buttonTintColor = .green
func showAlert() {
// Create and configure the alert then...
alert.appearanceDelegate = self
}
}
查看示例项目以获取更多复杂示例。
该项目遵守MIT许可协议。请参阅LICENSE文件。