AKAlertController
用 Swift 编写的简单 Alert 视图,可以作为 UIAlertController 的替代品使用。
它支持 iOS 10!它简单易于自定义!支持警报和动作表单!
易于使用
AKAlertController可以用作UIAlertController。
警告
let alertController = AKAlertController(title: "Title label", message: "Message label", preferredStyle: .alert)
let handler: (AKAlertAction) -> Void = { action in print("\(action.title) pressed" )}
alertController.addAction(AKAlertAction(title: "Default action", style: .default, handler: handler))
alertController.addAction(AKAlertAction(title: "Cancel action", style: .cancel, handler: handler))
alertController.addAction(AKAlertAction(title: "Destructive action", style: .destructive, handler: handler))
// add textfield
alertController.addTextFieldWithConfigurationHandler { textField in
textField.placeholder = "Password"
textField.secureTextEntry = true
}
present(alertController, animated: true, completion: nil)
操作表
let actionSheet = AKAlertController(title: "Title label", message: nil, preferredStyle: .actionSheet)
let handler: (AKAlertAction) -> Void = { action in print("\(action.title) pressed" )}
actionSheet.addAction(AKAlertAction(title: "Default action", style: .default, handler: handler))
actionSheet.addAction(AKAlertAction(title: "Cancel action", style: .cancel, handler: handler))
actionSheet.addAction(AKAlertAction(title: "Destructive action", style: .destructive, handler: handler))
present(actionSheet, animated: true, completion: nil)
可自定义元素
AKAlertController完全复现了系统UIAlertController接口,但增加了可自定义的能力
- 字体(文本、按钮)
- 颜色(覆盖层、视图、文本、按钮)
- 尺寸
- 可以添加图片
控制器级别的自定义功能可用
var appearance = AKAlertControllerAppearance.defaultAppearance()
appearance.titleFont = .systemFont(ofSize: 22)
appearance.titleTextColor = .purple
let alertController = AKAlertController(title: "Title", message: "Message", preferredStyle: .alert, appearance: appearance)
...
因此对于每一个动作
...
let cancelAction = AKAlertAction(title: "Cancel", style: .cancel, handler: handler)
cancelAction.bgColor = .black
cancelAction.textColor = .white
cancelAction.icon = UIImage(named: "close")
alertController.addAction(cancelAction)
...
带图片的示例
let alertController = AKAlertController(title: "Title label", message: "Message label", headerImage: UIImage(named: "close"), preferredStyle: .alert)
let handler: (AKAlertAction) -> Void = { action in print("\(action.title) pressed" )}
alertController.addAction(AKAlertAction(title: "Default action", style: .default, handler: handler))
alertController.addAction(AKAlertAction(title: "Cancel action", style: .cancel, handler: handler))
alertController.addAction(AKAlertAction(title: "Destructive action", style: .destructive, handler: handler))
present(alertController, animated: true, completion: nil)
示例
要运行示例项目,首先克隆仓库,然后从示例目录中运行 pod install
。
要求
- iOS 10.0+
- Swift 5.0+
- Xcode 11+
安裝
AKAlertController 可以通過 CocoaPods 获取。安裝它,只需將以下行添加到您的 Podfile 中:
pod 'AKAlertController', '~> 1.1.0'
许可证
AKAlertController 可在 MIT 许可证下使用。有关更多信息,请参阅 LICENSE 文件。