iOS技术点iOS编程精华收录

提醒对话框SPAlertController官方文档(iOS)

2019-01-03  本文已影响159人  S型身材的猪

SPAlertControllergithub地址:https://github.com/SPStore/SPAlertController

前言

CocoaPods

platform:ios,'9.0'
target 'MyApp' do
pod 'SPAlertController', '~> 3.0.1'
end

弹出对话框举例

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:^{}];

如果使用的是SPAlertControllerStyleActionSheet样式,将会默认从底部弹出,如果使用的是SPAlertControllerStyleAlert样式,则默认以缩放动画(由大到小)从中间弹出,并且SPAlertControllerStyleAlert下可以添加文本输入框

Topics

创建SPAlertController

+ (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样式,默认动画为从中间弹出


SPAlertController的头部配置


SPAlertControllerd的action配置

- (void)addAction:(SPAlertAction *)action;

@property (nonatomic, readonly) NSArray<SPAlertAction *> *actions;

添加action,actions里面存放的就是添加的所有action


- (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler;

@property(nullable, nonatomic, readonly) NSArray<UITextField *> *textFields;

添加文本输入框,textFields存放的就是添加的所有textField


09FCBF2A406195883C9EC5499374C189.jpg
@property(nonatomic) UILayoutConstraintAxis actionAxis;

该属性配置的是对话框action的排列方式,分垂直排列和水平排列; SPAlertControllerStyleActionSheet样式下:默认为UILayoutConstraintAxisVertical(垂直排列), 如果设置为UILayoutConstraintAxisHorizontal(水平排列),则除去取消样式action之外的其余action将水平排列;SPAlertControllerStyleAlert样式下:当actions的个数大于2,或者某个action的title显示不全时为UILayoutConstraintAxisVertical(垂直排列),否则默认为UILayoutConstraintAxisHorizontal(水平排列),此样式下设置该属性可以修改所有action的排列方式;不论哪种样式,只要外界设置了该属性,永远以外界设置的优先


@property(nonatomic, assign) CGFloat minDistanceToEdges;

该属性配置的是距离屏幕边缘的最小间距;SPAlertControllerStyleAlert样式下该属性是指对话框四边与屏幕边缘之间的距离,此样式下默认值随设备变化,SPAlertControllerStyleActionSheet样式下是指弹出边的对立边与屏幕之间的距离,比如如果从右边弹出,那么该属性指的就是对话框左边与屏幕之间的距离,此样式下默认值为70


@property(nonatomic, assign) BOOL needDialogBlur;

该属性是制造对话框的毛玻璃效果,3.0版本开始采用的是系统私有类_UIDimmingKnockoutBackdropView所实现


@property(nonatomic, assign) CGPoint offsetForAlert;

- (void)setOffsetForAlert:(CGPoint)offsetForAlert animated:(BOOL)animated;

SPAlertControllerStyleAlert下的偏移量配置 ,CGPoint类型,y值为正向下偏移,为负向上偏移;x值为正向右偏移,为负向左偏移,该属性只对SPAlertControllerStyleAlert样式有效,键盘的frame改变会自动偏移,如果外界手动设置偏移只会取手动设置的


- (void)setCustomSpacing:(CGFloat)spacing afterAction:(SPAlertAction *)action API_AVAILABLE(ios(11.0));

- (CGFloat)customSpacingAfterAction:(SPAlertAction *)action API_AVAILABLE(ios(11.0));

该API是设置和获取指定action后面的间距,如图中箭头所指,iOS11及其以上才支持


3CB7E93FDA241F253BFE156D0B4AA7E2.jpg
- (void)setBackgroundViewAppearanceStyle:(SPBackgroundViewAppearanceStyle)style alpha:(CGFloat)alpha;

该API是设置背景蒙层的样式,分为半透明和毛玻璃效果,毛玻璃又细分为Dark,ExtraLight,Light3种样式


6ABFFFFADAAAE68B0678A82839B864E8.jpg
@property(nonatomic, assign) BOOL tapBackgroundViewDismiss;

单击背景蒙层是否退出对话框,默认YES


@property(nonatomic, assign) CGFloat cornerRadiusForAlert;

SPAlertControllerStyleAlert下的圆角半径


创建action

+ (instancetype)actionWithTitle:(nullable NSString *)title style:(SPAlertActionStyle)style handler:(void (^ __nullable)(SPAlertAction *action))handler;

其中,title为action的标题,创建的时候仅支持普通文本,如果要使用富文本,可以另外设置action的属性attributedTitle,设置后会覆盖普通文本


配置action

自定义各大View

+ (instancetype)alertControllerWithCustomHeaderView:(nullable UIView *)customHeaderView preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType;
C874091BE0B4C7A16105726BA5029F9B.jpg
+ (instancetype)alertControllerWithCustomAlertView:(nullable UIView *)customAlertView preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType;
10711A73856A0C4283EB5024A2C64587.jpg
+ (instancetype)alertControllerWithCustomActionSequenceView:(nullable UIView *)customActionSequenceView title:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType;
AB29E246B855E6A4F87C18631C2BFFDE.jpg

关于自定义view的大小

上一篇 下一篇

猜你喜欢

热点阅读