UIAlertController
的自定义替代品,功能更强大
实际上,您可以简单地用PopupViewController
替换UIAlertController
,用PopupAction
替换UIAlertAction
,这样就完成了。目前不支持Action Sheet,只支持alert模式。
简单示例。
let alert = PopupViewController(title: "Alert title", message: "Alert message, which can be very long and etc....")
alert.addAction(PopupAction(title: "Ok", type: .positive, handler: nil))
present(alert, animated: true, completion: nil)
结果
默认情况下,它带有深色主题,但强大之处在于您还可以自定义它。
示例
var customizable = PopupViewController.Customizable()
customizable.titleColor = UIColor.blue
customizable.positiveActionColor = UIColor.red
customizable.messageColor = UIColor.brown
customizable.messageFont = UIFont.boldSystemFont(ofSize: 22)
customizable.negativeActionColor = UIColor.brown
customizable.positiveActionColor = UIColor.blue
customizable.negativeActionBackgroundColor = UIColor.black
customizable.positiveActionBackgroundColor = UIColor.white
customizable.positiveActionHighlightColor = UIColor.green
customizable.negativeActionHighlightColor = UIColor.red
let alert = PopupViewController(title: "Alert title",
message: "Alert message, which can be very long message and all that but nobody will ever read it.",
customizable: customizable)
alert.blurStyle = .extraLight
alert.addAction(PopupAction(title: "Ok", type: .positive, handler: nil))
alert.addAction(PopupAction(title: "Cancel", type: .negative, handler: nil))
present(alert, animated: true, completion: nil)
结果
是的,这看起来很丑。但你知道...
您还可以设置一个静态的Customizable
,如果您在PopupViewController
构造函数中不传递任何自定义控件,它将重复使用。
var customizable = PopupViewController.Customizable()
customizable.titleColor = UIColor.blue
customizable.positiveActionColor = UIColor.red
customizable.messageColor = UIColor.brown
customizable.messageFont = UIFont.boldSystemFont(ofSize: 22)
customizable.negativeActionColor = UIColor.brown
customizable.positiveActionColor = UIColor.blue
customizable.negativeActionBackgroundColor = UIColor.black
customizable.positiveActionBackgroundColor = UIColor.white
customizable.positiveActionHighlightColor = UIColor.green
customizable.negativeActionHighlightColor = UIColor.red
PopupViewController.sharedCustomizable = alertCustomizable
在您的Podfile中添加pod 'PopupViewController'
并运行pod install
。
克隆此仓库,并在PopupViewController和Example目录中运行pod install
。
由于我是一个懒人,不想查看苹果文档中的丑陋的Autolayout代码提示,目前它使用惊人的Cartography作为依赖项。