本README是从原创作者那里Copy过来的,然后修改为Swift版本 Objective-C版本
把SPAlertController
文件夹拖拽到项目中直接使用
#git "https://github.com/dongxiexidu/SPAlertController.git" "master"
import SPAlertController
let alertController = SPAlertController.alertController(withTitle: "我是主标题", message: "我是副标题", preferredStyle: .actionSheet)
let action1 = SPAlertAction.action(withTitle: "Default", style: .default) { (action) in
print("点击了Default")
}
let action2 = SPAlertAction.action(withTitle: "Destructive", style: .destructive) { (action) in
print("点击了Destructive")
}
let action3 = SPAlertAction.action(withTitle: "Cancel", style: .cancel) { (action) in
print("点击了Cancel------")
}
alertController.addAction(action: action1)
alertController.addAction(action: action3)
alertController.addAction(action: action2)
self.present(alertController, animated: true, completion: nil)
public class func alertController(withTitle title: String?,
message: String?,
preferredStyle: SPAlertControllerStyle)
->SPAlertController
public class func alertController(withTitle title: String?,
message: String?,
preferredStyle: SPAlertControllerStyle,
animationType: SPAlertAnimationType)
->SPAlertController
上面2种创建方式唯一的区别就是:第2种方式多了一个animationType参数,该参数可以设置弹出动画。如果以第一种方式创建,会采用默认动画,默认动画跟preferredStyle 有关,如果是SPAlertControllerStyle.actionSheet样式,默认动画为从底部弹出,如果是SPAlertControllerStyle.alert样式,默认动画为从中间弹出
// 添加action,actions里面存放的就是添加的所有action
public func addAction(action: SPAlertAction)
//MARK: TODO: readonly
public var actions: [SPAlertAction] = [SPAlertAction]()
// 添加文本输入框,textFields存放的就是添加的所有textField
public func addTextFieldWithConfigurationHandler(handler: ((UITextField)->Void)?)
var textFields = [UITextField]()
// SPAlertControllerStyleActionSheet样式下:默认为UILayoutConstraintAxisVertical(垂直排列),
如果设置为UILayoutConstraintAxisHorizontal(水平排列),则除去取消样式action之外的其余action将水平排列;
SPAlertControllerStyleAlert样式下:当actions的个数大于2,或者某个action的title显示不全时
为UILayoutConstraintAxisVertical(垂直排列),
否则默认为UILayoutConstraintAxisHorizontal(水平排列),此样式下设置该属性可以修改所有action的排列方式;
不论哪种样式,只要外界设置了该属性,永远以外界设置的优先
public var actionAxis: NSLayoutConstraint.Axis?
// 该属性配置的是距离屏幕边缘的最小间距;SPAlertControllerStyleAlert样式下该属性是指
对话框四边与屏幕边缘之间的距离,此样式下默认值随设备变化,SPAlertControllerStyleActionSheet
样式下是指弹出边的对立边与屏幕之间的距离,比如如果从右边弹出,
那么该属性指的就是对话框左边与屏幕之间的距离,此样式下默认值为70
public var minDistanceToEdges: CGFloat = 70
// 该属性是制造对话框的毛玻璃效果,采用的是系统私有类_UIDimmingKnockoutBackdropView所实现
public var needDialogBlur: Bool = false
// SPAlertControllerStyleAlert下的偏移量配置 ,CGPoint类型,y值为正向下偏移,
为负向上偏移;x值为正向右偏移,为负向左偏移,该属性只对SPAlertControllerStyleAlert样式有效,
键盘的frame改变会自动偏移,如果外界手动设置偏移只会取手动设置的
public var offsetForAlert: CGPoint = .zero
public func setOffsetForAlert(_ offsetForAlert: CGPoint, animated: Bool)
// 该API是设置和获取指定action后面的间距,如图中箭头所指,iOS11及其以上才支持
@objc @available(iOS 11.0, *)
public func setCustomSpacing(spacing: CGFloat, aferAction action: SPAlertAction?)
internal func customSpacingAfterActionIndex(_ index: Int) -> CGFloat
// 该API是设置背景蒙层的样式,分为半透明和毛玻璃效果,毛玻璃又细分为Dark,ExtraLight,Light3种样式
public func setBackgroundViewAppearanceStyle(_ style: SPBackgroundViewAppearanceStyle, alpha: CGFloat)
// 单击背景蒙层是否退出对话框,默认YES
public var tapBackgroundViewDismiss: Bool = true
// 圆角半径
public var cornerRadius: CGFloat = 6.0
// 其中,title为action的标题,创建的时候仅支持普通文本,如果要使用富文本,
// 可以另外设置action的属性attributedTitle,设置后会覆盖普通文本
public class func action(withTitle title: String?,
style: SPAlertActionStyle,
handler: @escaping (SPAlertAction)->Void) -> SPAlertAction
public class func alertController(withCustomHeaderView customHeaderView: UIView,
preferredStyle: SPAlertControllerStyle,
animationType: SPAlertAnimationType)
->SPAlertController {
}
public class func alertController(withCustomAlertView customAlertView: UIView,
preferredStyle: SPAlertControllerStyle,
animationType: SPAlertAnimationType)
->SPAlertController {
}
public class func alertController(withCustomActionSequenceView customActionSequenceView: UIView,
title: String,
message: String,
preferredStyle: SPAlertControllerStyle,
animationType: SPAlertAnimationType) -> SPAlertController{
}
- public func updateCustomViewSize(size: CGSize)
设置其大小;- public func updateCustomViewSize(size: CGSize)
通知SPAlertController更新其大小具体示例,可以下载demo查看
版本 | 更新日期 | 支持最低系统版本 | 更新内容 |
---|---|---|---|
v1.0 | 2020.01.15 | iOS9 | |
回到顶部 |