ZVAlertController
自定义 Swift 弹出控制器
ZVAlertController 是一个纯粹的 Swift 语言 AlertController。
要求
- iOS 9.0+
- Swift 5.0
安装
Cocoapod
CocoaPods 是用于 Swift 和 Objective-C Cocoa 项目的依赖管理器。
您可以使用以下命令安装 Cocoapod
$ sudo gem install cocoapods
要使用 CocoaPods 将 ZVAlertController
集成到您的项目中,在您的 Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
target 'TargetName' do
use_frameworks!
pod 'ZVAlertController'
end
然后,使用 CocoaPods 安装您的依赖项。
$ pod install
Carthage
Carthage 旨在为应用添加框架提供最简单的方式。
您可以使用 Homebrew 通过以下命令安装 Carthage:
$ brew update
$ brew install carthage
要使用 Carthage 将 ZVAlertController
集成到您的项目中,请在您的 Cartfile
中指定它。
github "zevwings/ZVAlertController" ~> 0.0.1
然后,使用 carthage update
命令构建框架,并将 ZVAlertController.framework
拖到您的项目中。
注意
框架位于 Carthage/Build 目录下,您应该将其拖到 Target
-> 通用
-> 嵌入的二进制文件
中。
使用方法
当您需要使用 ZVAlertController
时,可以:import ZVAlertController
还是 import AlertController
。
显示 Alert
当您启动一个任务时,可以使用以下代码:
let alertController = AlertController(title: "title", message: "message")
present(alertController, animated: true, completion: nil)
您可以使用以下方式添加 alert 动作:
alertController.addAction(AlertAction(title: "confirm", style: .default, handler: { _ in
print("comfirm action")
}))
您也可以在消息正文中使用 NSAttributedString
。
let attributedString = NSMutableAttributedString(string: "message")
attributedString.addAttributes([.foregroundColor: UIColor.cyan], range: NSRange(location: 0, length: 4))
let alertController = AlertController(title: "title", message: attributedString)
属性
以下属性可以自定义 alertController。
// dismiss completion handler
public var dismissHandler: DismissHandler?
// should show the dissmiss button
public var shouldShowDismissButton: Bool = true
// dismiss button content color
public var tintColor: UIColor = .blue
// destructive and default button text color
public var positiveColor: UIColor = Constant.positiveColor
// cancel button text color
public var negativeColor: UIColor = Constant.negativeColor
// action button text font
public var buttonFont: UIFont = .systemFont(ofSize: 14.0)
// title text color
public var titleColor: UIColor = Constant.titleColor
// title text color
public var titleFont: UIFont = .systemFont(ofSize: 14.0)
// message body text color
public var messageColor: UIColor = Constant.messageColor
// message body text font
public var messageFont: UIFont = .systemFont(ofSize: 14.0)
动画
ZVAlertController
还提供了 AlertPresentStyle
和 AlertDismissStyle
用于动画,您可以使用 presentStyle
或 dismissStyle
设置显示或隐藏动画。
alertController.presentStyle = .fadeIn
alertController.dismissStyle = .fadeOut
更多用法,请参阅 示例
许可协议
ZVAlertController
在 Apache 许可协议的条款和条件下进行分发