- 采用VFL布局,3.0版本开始,核心控件为UIStackView,风格与微信几乎零误差
- 3.0版本开始,对话框头部新增图片设置
- 3.0版本开始,action支持图片设置
- 3.0版本开始,iOS11及其以上系统可单独设置指定action后的间距
- 3.0版本开始,支持富文本
- action的排列可灵活设置垂直排列和水平排列
- 每个action的高度自适应
- 支持旋转(横竖屏)
- 支持自定义头部视图
- 支持头部和action之间插入自定义视图
- 支持对话框毛玻璃和背景蒙层毛玻璃
- 全面适配iPhoneX,iPhoneXR,iPhoneXS,iPhoneXS MAX
- 全面适配深色模式
platform:ios,'9.0'
target 'MyApp' do
pod 'SPAlertController', '~> 4.1.0'
end
Swift Package Manager是Apple官方提供的依赖管理工具,在Xcode中集成。
- Xcode 11+,打开你的项目,选择:File > Swift Packages > Add Package Dependency
- 输入仓库地址:https://github.com/SPStore/SPAlertController
- 选择版本规则:Up to Next Major(4.1.0)
SPAlertController *alert = [SPAlertController alertControllerWithTitle:@"我是主标题" message:@"我是副标题" preferredStyle:SPAlertControllerStyleActionSheet];
SPAlertAction *action1 = [SPAlertAction actionWithTitle:@"Default" style:SPAlertActionStyleDefault handler:^(SPAlertAction * _Nonnull action) {}];
SPAlertAction *action2 = [SPAlertAction actionWithTitle:@"Destructive" style:SPAlertActionStyleDestructive handler:^(SPAlertAction * _Nonnull action) {}];
SPAlertAction *action3 = [SPAlertAction actionWithTitle:@"Cancel" style:SPAlertActionStyleCancel handler:^(SPAlertAction * _Nonnull action) {}];
[alert addAction:action1];
[alert addAction:action2];
[alert addAction:action3];
[self presentViewController: alert animated:YES completion:^{}];
+ (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(SPAlertControllerStyle)preferredStyle;
+ (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType;
上面2种创建方式唯一的区别就是:第2种方式多了一个animationType参数,该参数可以设置弹出动画。如果以第一种方式创建,会采用默认动画,默认动画跟preferredStyle 有关,如果是SPAlertControllerStyleActionSheet样式,默认动画为从底部弹出,如果是SPAlertControllerStyleAlert样式,默认动画为从中间弹出
- title,对话框的主标题
- message,对话框的副标题
- attributedTitle,对话框的主标题,富文本
- attributedMessage,对话框的副标题,富文本
- image,对话框的头部上的图标,位置处于主标题之上
- titleColor,对话框主标题的颜色
- titleFont,对话框主标题的字体
- messageColor,对话框副标题的颜色
- messageFont,对话框副标题的字体
- textAlignment,对话框标题的对齐方式(标题指主标题和副标题)
- imageLimitSize,对话框头部图标的限制大小,默认是无穷大
// 添加action,actions里面存放的就是添加的所有action
- (void)addAction:(SPAlertAction *)action;
@property (nonatomic, readonly) NSArray<SPAlertAction *> *actions;
// 添加文本输入框,textFields存放的就是添加的所有textField
- (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler;
@property(nullable, nonatomic, readonly) NSArray<UITextField *> *textFields;
// SPAlertControllerStyleActionSheet样式下:默认为UILayoutConstraintAxisVertical(垂直排列), 如果设置为UILayoutConstraintAxisHorizontal(水平排列),则除去取消样式action之外的其余action将水平排列;SPAlertControllerStyleAlert样式下:当actions的个数大于2,或者某个action的title显示不全时为UILayoutConstraintAxisVertical(垂直排列),否则默认为UILayoutConstraintAxisHorizontal(水平排列),此样式下设置该属性可以修改所有action的排列方式;不论哪种样式,只要外界设置了该属性,永远以外界设置的优先
@property(nonatomic) UILayoutConstraintAxis actionAxis;
- SPAlertControllerStyleActionSheet样式下的垂直排列和水平排列
- SPAlertControllerStyleAlert样式下的垂直排列和水平排列
// 该属性配置的是距离屏幕边缘的最小间距;SPAlertControllerStyleAlert样式下该属性是指对话框四边与屏幕边缘之间的距离,此样式下默认值随设备变化,SPAlertControllerStyleActionSheet样式下是指弹出边的对立边与屏幕之间的距离,比如如果从右边弹出,那么该属性指的就是对话框左边与屏幕之间的距离,此样式下默认值为70
@property(nonatomic, assign) CGFloat minDistanceToEdges;
// 该属性是制造对话框的毛玻璃效果,3.0版本开始采用的是系统私有类_UIDimmingKnockoutBackdropView所实现
@property(nonatomic, assign) BOOL needDialogBlur;
// SPAlertControllerStyleAlert下的偏移量配置 ,CGPoint类型,y值为正向下偏移,为负向上偏移;x值为正向右偏移,为负向左偏移,该属性只对SPAlertControllerStyleAlert样式有效,键盘的frame改变会自动偏移,如果外界手动设置偏移只会取手动设置的
@property(nonatomic, assign) CGPoint offsetForAlert;
- (void)setOffsetForAlert:(CGPoint)offsetForAlert animated:(BOOL)animated;
// 该API是设置和获取指定action后面的间距,如图中箭头所指,iOS11及其以上才支持
- (void)setCustomSpacing:(CGFloat)spacing afterAction:(SPAlertAction *)action API_AVAILABLE(ios(11.0));
- (CGFloat)customSpacingAfterAction:(SPAlertAction *)action API_AVAILABLE(ios(11.0));
// 该API是设置背景蒙层的样式,分为半透明和毛玻璃效果,毛玻璃又细分为Dark,ExtraLight,Light3种样式
- (void)setBackgroundViewAppearanceStyle:(SPBackgroundViewAppearanceStyle)style alpha:(CGFloat)alpha;
// 单击背景蒙层是否退出对话框,默认YES
@property(nonatomic, assign) BOOL tapBackgroundViewDismiss;
@property(nonatomic, assign) CGFloat cornerRadiusForAlert;
SPAlertControllerStyleAlert下的圆角半径
// 其中,title为action的标题,创建的时候仅支持普通文本,如果要使用富文本,可以另外设置action的属性attributedTitle,设置后会覆盖普通文本
+ (instancetype)actionWithTitle:(nullable NSString *)title style:(SPAlertActionStyle)style handler:(void (^ __nullable)(SPAlertAction *action))handler;
- title,action的标题
- attributedTitle,action的富文本标题,普通文本和富文本同时设置时,只会显示富文本
- image,action上的图片,文字和图片都存在时,图片在左,文字在右
- imageTitleSpacing,图片与文字之间的间距
- titleColor,action标题的颜色
- titleFont,action标题的字体
- titleEdgeInsets,action标题的内边距,此属性能够改变action的高度
- enabled,action是否能被点击
- 自定义对话框的头部
+ (instancetype)alertControllerWithCustomHeaderView:(nullable UIView *)customHeaderView preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType;
- 自定义整个对话框
+ (instancetype)alertControllerWithCustomAlertView:(nullable UIView *)customAlertView preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType;
- 自定义对话框的action部分
+ (instancetype)alertControllerWithCustomActionSequenceView:(nullable UIView *)customActionSequenceView title:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType;
版本 | 更新日期 | 支持最低系统版本 | 更新内容 |
---|---|---|---|
v4.1.0 | 2025.09.09 | iOS9.0 | 1、修复title和message同时为空的情况下,在部分机型上无法弹出的问题 2、action事件回调改为dimiss动画完成之后 3、action新增allowsAutoDismiss和backgroundColor属性 4、修复深色模式下模糊效果失效问题 5、支持SPM |
v4.0.0 | 2020.04.27 | iOS9.0 | 适配深色模式 |
v3.1.0 | 2019.08.08 | iOS9.0 | 1、actionSheet样式加了单边圆角效果 2、毛玻璃默认为NO 3、当不需要毛玻璃效果时,actionButton的选中背景色修改(灰度0.1),属于bug修复 4、“取消action”上的分割线背景色修改(灰度由0.3变为0.2),高度由6增加为8 5、每个actionButton的最小高度由49变为55 |
v3.0.4 | 2019.06.03 | iOS9.0 | 修复了tapBackgroundViewDismiss属性无效问题 |
v3.0.3 | 2019.05.30 | iOS9.0 | 修复了action点击的回调为nil时闪退问题 |
v3.0.2 | 2019.05.09 | iOS9.0 | 修复了内存泄漏问题 |
v3.0.1 | 2019.01.03 | iOS9.0 | 背景蒙层动画更加的柔和 |
v3.0.0 | 2018.12.29 | iOS9.0 | 全方位的大重构 |
v2.5.2 | 2018.12.03 | iOS8.0 | 1、按钮处于最底部时长按touchDown事件的延时现象采用新方式解决 2、修复了内存泄露问题 |
v2.5.1 | 2018.11.07 | iOS8.0 | 1、修改了action的选中效果 2、解决iOS11之后,按钮处于最底部时长按touchDown事件的延时现象 |
v2.5.0 | 2018.11.07 | iOS8.0 | 1、毛玻璃思路另辟蹊径,毛玻璃效果不会受到背景遮罩的影响,同时背景遮罩不再被镂空 2、增加动画枚举,可以从左右弹出 3、自定义view时的背景色改为透明色 4、增加actionHeight属性,修复maxNumberOfActionHorizontalArrangementForAlert属性 5、去除了中间tableView的最后一条分割线 6、修改了分割线的颜色,更加接近微信原生 7、修复centerView上有textView/textField,旋转屏幕后不可见问题 8、优化代码 |
v2.1.1 | 2018.11.01 | iOS8.0 | |
v2.1.0 | 2018.10.18 | iOS8.0 | |
v2.0.0 | 2017.11.19 | iOS8.0 | |
v1.7.0 | 2017.11.06 | iOS8.0 | |
v1.5.0 | 2017.11.06 | iOS8.0 | |
v1.0.1 | 2017.10.24 | iOS8.0 | |
v1.0.0 | 2017.10.24 | iOS8.0 |