CDAlertView 是一个高度可定制的alert弹出视图,用Swift编写。使用方式类似于 UIAlertController
。
截图
动画
使用方式
无按钮的基本使用
CDAlertView(title: "Awesome Title", message: "Well explained message!", type: .notification).show()
注意: 您可以在没有按钮的情况下使用它。触摸弹出视图外部或移动它,如果没有操作按钮,则会消失。如果有操作按钮,则只有按按钮它才会消失。
添加新按钮
let alert = CDAlertView(title: "Awesome Title", message: "Are you in?!", type: .notification)
let doneAction = CDAlertViewAction(title: "Sure! 💪")
alert.add(action: doneAction)
let nevermindAction = CDAlertViewAction(title: "Nevermind 😑")
alert.add(action: nevermindAction)
alert.show()
启用弹出视图中的文本字段
alert.isTextFieldHidden = false
也支持自定义视图。如果您想使用自定义视图,则负责自定义视图的高度。自定义视图在 UIStackView
中使用。
let myCustomView = UIVIew(frame: myFrame)
// Don't forget to handle height of `myCustomView`
alert.customView = myCustomView
CDAlertView 类型
public enum CDAlertViewType {
case error, warning, success, notification, alarm, noImage, custom(image:UIImage)
}
初始化
高级警报初始化
要使用自定义图标,请不指定类型(或使用 .empty)并设置图标和背景颜色
let alert = CDAlertView(title: "Awesome Title", message: "Well explained message!", type: .custom(image: UIImage(named:"YourAwesomeImage")))
alert.circleFillColor = UIColor.yourAmazingColor
使用你的动画隐藏警报
let alert = CDAlertView(title: "Awesome Title", message: "Well explained message!", type: .success)
alert.hideAnimations = { (center, transform, alpha) in
transform = CGAffineTransform(scaleX: 3, y: 3)
alpha = 0
}
alert.hideAnimationDuration = 0.88
alert.show()
使用定时器隐藏警报
let alert = CDAlertView(title: "Awesome Title", message: "Well explained message!", type: .success)
alert.autoHideTime = 4.5 // This will hide alert box after 4.5 seconds
可用的 CDAlertView 选项列表
titleTextColor: UIColor
-> 设置标题的文本颜色
messageTextColor: UIColor
-> 设置消息的文本颜色
titleFont: UIFont
-> 设置标题的字体
messageFont: UIFont
-> 设置消息的字体
isHeaderIconFilled: Bool
-> 选择填充图标而不是轮廓图标。默认为 false
。
alertBackgroundColor: UIColor
-> 设置弹出窗口的背景颜色。
popupWidth: CGFloat
-> 弹出视图宽度
hasRoundedCorners: Bool
-> 给警报视图应用圆角。默认为 true
。
hasShadow: Bool
-> 在弹出窗口周围应用阴影。默认为 true
。
circleFillColor: UIColor
-> 设置页眉图标的背景颜色。(圆形区域的颜色)
isActionButtonsVertical: Bool
-> 垂直对齐操作按钮。默认为 false
。水平按钮的最大数量为 3。
canHideWhenTapBack
-> 当点击背景视图时隐藏自己。默认为 false
。
hideAnimationDuration: TimeInterval
-> 设置隐藏动画的持续时间
hideAnimations: CDAlertAnimationBlock
-> 根据中心的 center
,transform
和 alpha
值设置隐藏动画。您可以通过更改这些值来创建自己的动画,以应用于警报弹出窗口。
如果您启用了设置 isTextFieldHidden
属性为 false
的文本字段,以下属性也将可用
textFieldFont: UIFont
-> textField 文本的字体
textFieldIsSecureTextEntry: Bool
-> 设置 UITextField
的 isSecureTextEntry
属性
textFieldReturnKeyType: UIReturnKeyType
-> 设置 UITextField
的 returnKeyType
属性
textFieldTextAlignment: NSTextAlignment
-> 设置 UITextField
的 textAlignment
属性。默认值是 .left
。
textFieldPlaceholderText: String?
-> 设置 UITextField
的占位文本。
textFieldAutocapitalizationType: UITextAutocapitalizationType
-> 设置 UITextField
的 autocapitalizationType
属性。默认值是 .none
。
textFieldBackgroundColor: UIColor
-> 设置 UITextField
的背景颜色。
textFieldTintColor: UIColor
-> 设置 UITextField
的着色颜色。
textFieldText: String?
-> 设置 & 获取 UITextField
的文本。
textFieldHeight: CGFloat
-> 设置 UITextField
的高度。
textFieldDelegate: UITextViewDelegate?
-> 设置 UITextField
的代理。默认代理是 CDAlertView
。如果您覆盖了它,您需要负责让 UITextField
失去焦点。
autoHideTime: TimeInterval?
-> 设置 dismiss 的时间间隔。默认值是 nil
。
高级动作初始化
font
、textColor
、backgroundColor
、handler
都是可选的,并且有默认参数值。您可以初始化它们或初始化后设置它们。
let action = CDAlertViewAction(title: "Action Title", font: UIFont.yourCustomFont, textColor: UIColor.yourTextColor, backgroundColor: UIColor.yourBackgroundColor, handler: { action in })
alertView.addAction(action)
注意: 按钮可以垂直和水平对齐。但水平放置超过3个按钮是不可能的。
CDAlertViewAction 选项列表
buttonTitle: String
-> 设置动作按钮标题
buttonTextColor: UIColor
-> 设置动作按钮标题颜色。默认值为 RGB(27,169,225)。
buttonFont: UIFont
-> 设置动作按钮标题字体。默认值是 UIFont.systemFont(ofSize: 17)
。
buttonBackgroundColor: UIColor
-> 设置动作按钮的背景颜色。如果不设置,将使用 CDAlertView 的 alertBackgroundColor
。
可用方法列表
textFieldBecomeFirstResponder()
-> 如果 alert.isTextFieldHidden
设置为 true
,则调用 textField
的 becomeFirstResponder()
方法。否则,不进行任何操作。
textFieldResignFirstResponder()
-> 如果 alert.isTextFieldHidden
设置为 true
,则调用 textField
的 resignFirstResponder()
方法。否则,不进行任何操作。
示例
要运行示例项目,首先从Example目录克隆仓库,然后运行pod install
。
安装
此库支持Swift 5。对于Swift 4.2请使用版本0.9.1
,对于Swift 3.1请使用版本0.6.1
。
CocoaPods
使用CDAlertView可通过CocoaPods获得。要安装它,只需将以下行添加到您的Podfile
中
pod "CDAlertView"
Carthage
使用CDAlertView可通过Carthage获得。要安装它,只需将以下行添加到您的Cartfile
中
github "candostdagdeviren/CDAlertView"
需求
- Xcode 9
- Swift 4
- iOS 9.0+
图标
感谢Icons8提供美丽的图标。
许可证
CDAlertView 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。