iOS-简约系统风格自由定制的弹窗Alert&ActionSheet

先上效果图

SLAlertView.gif

再上demo地址 如果觉得好用请给star鼓励 谢谢

github demo

项目介绍

  • 这是一款简约系统风格的弹窗,开发者可通过一句代码创建并弹出。
  • 开发者可以选择通过代理的方式或者block的方式来监听按钮的点击事件。
  • 开发者可通过settingHandler设置弹窗的背景颜色、字体颜色、字体以及分割线的颜色以满足不同风格的app的需求
  • 更加详尽的使用情况请下载demo

使用说明

安装

将demo中的SLAlertView文件夹拖入项目


SLAlertView文件夹.png

接口说明

/**
 *  创建并弹出使用代理监听点击事件的默认的alertView
 *  param title   标题
 *  param message   提示文字内容
 *  param delegate  代理 可通过设置代理监听按钮的点击
 *  param preferredStyle    弹出样式
 *  param cancelButtonTitle     取消按钮文字
 *  param otherButtonTitles     其他按钮文字(数组)@[@"other1",@"other2"]
 */
+ (void)alertViewWithTitle:(nullable NSString *)title
                   message:(nullable NSString *)message
                  delegate:(nullable id<SLAlertViewProtocol>)delegate
            preferredStyle:(SLAlertViewStyle)preferredStyle
         cancelButtonTitle:(nullable NSString *)cancelButtonTitle
         otherButtonTitles:(nullable NSArray *)otherButtonTitles;




/**
 *  创建并弹出使用block监听点击事件的默认的alertView
 *  param title   标题
 *  param message   提示文字内容
 *  param preferredStyle    弹出样式
 *  param cancelButtonTitle     取消按钮文字
 *  param otherButtonTitles     其他按钮文字(数组)@[@"other1",@"other2"]
 *  param clickHandler     点击按钮的回调
 */
+ (void)alertViewWithTitle:(nullable NSString *)title
                   message:(nullable NSString *)message
            preferredStyle:(SLAlertViewStyle)preferredStyle
         cancelButtonTitle:(nullable NSString *)cancelButtonTitle
         otherButtonTitles:(nullable NSArray *)otherButtonTitles
              clickHandler:(nullable void (^)(SLAlertView * _Nonnull alertView,NSInteger buttonIndex,NSString * _Nullable buttonTitle))clickHandler;





/**
 *  创建并弹出使用代理监听点击事件的可自定义的alertView
 *  param title   标题
 *  param message   提示文字内容
 *  param delegate  代理 可通过设置代理监听按钮的点击
 *  param preferredStyle    弹出样式
 *  param cancelButtonTitle     取消按钮文字
 *  param otherButtonTitles     其他按钮文字(数组)@[@"other1",@"other2"]
 *  param settingHandler    自定义设置的回调 可通过此block设置各种背景颜色、字体颜色和字体。
    自定义设置方式:
    alertView.titleColor = [UIColor blackColor];
    alertView.messageColor = [UIColor blackColor];
    ...
 */
+ (void)alertViewWithTitle:(nullable NSString *)title
                   message:(nullable NSString *)message
                  delegate:(nullable id<SLAlertViewProtocol>)delegate
            preferredStyle:(SLAlertViewStyle)preferredStyle
         cancelButtonTitle:(nullable NSString *)cancelButtonTitle
         otherButtonTitles:(nullable NSArray *)otherButtonTitles
            settingHandler:(nullable void (^)( SLAlertView * _Nonnull alertView))settingHandler;




/**
 *  创建并弹出使用block监听点击事件的可自定义的alertView
 *  param title   标题
 *  param message   提示文字内容
 *  param preferredStyle    弹出样式
 *  param cancelButtonTitle     取消按钮文字
 *  param otherButtonTitles     其他按钮文字(数组)@[@"other1",@"other2"]
 *  param settingHandler    自定义设置的回调 可通过此block设置各种背景颜色、字体颜色和字体。
    自定义设置方式:
    alertView.titleColor = [UIColor blackColor];
    alertView.messageColor = [UIColor blackColor];
    ...
 *  param clickHandler     点击按钮的回调
 */
+ (void)alertViewWithTitle:(nullable NSString *)title
                   message:(nullable NSString *)message
            preferredStyle:(SLAlertViewStyle)preferredStyle
         cancelButtonTitle:(nullable NSString *)cancelButtonTitle
         otherButtonTitles:(nullable NSArray *)otherButtonTitles
            settingHandler:(nullable void (^)( SLAlertView * _Nonnull alertView))settingHandler
              clickHandler:(nullable void (^)(SLAlertView * _Nonnull alertView,NSInteger buttonIndex,NSString * _Nullable buttonTitle))clickHandler;


// 代理方法
- (void)alertView:(SLAlert * _Nonnull)alertView didSelectedButtonWithButtonIndex:(NSInteger)index buttonTitle:(NSString * _Nullable)buttonTitle;
- (void)actionSheet:(SLActionSheet * _Nonnull)actionSheet didSelectedButtonWithButtonIndex:(NSInteger)index buttonTitle:(NSString * _Nullable)buttonTitle;

调用

  • 导入头文件
#import "SLAlertView.h"
  • 在需要弹窗的地方调用接口
    1> 创建并弹出使用代理监听点击事件的默认的alert / actionSheet
// alert
[SLAlertView alertViewWithTitle:@"title"
                            message:@"messagemessagemessagemessagemessagemessagemessagemessaemessagemessage"
                           delegate:self
                     preferredStyle:SLAlertViewStyleAlert
                  cancelButtonTitle:@"cancel"
                  otherButtonTitles:nil];

 // action sheet
    [SLAlertView alertViewWithTitle:@"title"
                            message:@"this is message"
                           delegate:self
                     preferredStyle:SLAlertViewStyleActionSheet
                  cancelButtonTitle:@"cancel"
                  otherButtonTitles:@[@"other"]];

2> 创建并弹出使用block监听点击事件的默认的alert / actionSheet

// 通过block回调方式创建alert
    [SLAlertView alertViewWithTitle:@"title"
                            message:@"message"
                     preferredStyle:SLAlertViewStyleAlert
                  cancelButtonTitle:@"cancel"
                  otherButtonTitles:@[@"other"]
                       clickHandler:^(SLAlertView * _Nonnull alertView, NSInteger buttonIndex, NSString * _Nullable buttonTitle) {
        
        NSLog(@"block %ld-%@",buttonIndex,buttonTitle);
        
    }];
// 通过block回调方式创建action sheet
    [SLAlertView alertViewWithTitle:@"actionSheet"
                            message:@"sheet_default"
                     preferredStyle:SLAlertViewStyleActionSheet
                  cancelButtonTitle:@"cancel"
                  otherButtonTitles:@[@"other1",@"other2",@"other3"]
                       clickHandler:^(SLAlertView * _Nonnull alertView, NSInteger buttonIndex, NSString * _Nullable buttonTitle) {
         NSLog(@"block sheet_default %ld-%@",buttonIndex,buttonTitle);
    }];

3> 自定义的情况(以actionSheet为例,alert用法完全一样)

 [SLAlertView alertViewWithTitle:@"title"
                            message:@"message"
                     preferredStyle:SLAlertViewStyleActionSheet
                  cancelButtonTitle:@"cancel"
                  otherButtonTitles:@[@"other1",@"other2"]
                     settingHandler:^(SLAlertView * _Nonnull alertView) {
        
                         alertView.titleBackgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
                         alertView.messageBackgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
                         alertView.otherBackgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
                         alertView.cancelBackgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
                         alertView.titleColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
                         alertView.messageColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
                         alertView.otherTitleColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
                         alertView.cancelTitleColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
                         alertView.separatorColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
                         alertView.titleFont = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:16];
                         alertView.messageFont = [UIFont fontWithName:@"AmericanTypewriter" size:16];
                         alertView.otherTitleFont = [UIFont fontWithName:@"DBLCDTempBlack" size:16];
                         alertView.cancelTitleFont = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:16];
    }
                       clickHandler:^(SLAlertView * _Nonnull alertView, NSInteger buttonIndex, NSString * _Nullable buttonTitle) {
        
        NSLog(@"block sheet_customBackgroundColor %ld-%@",buttonIndex,buttonTitle);
        
    }];

4> 代理方法。遵守协议 <SLAlertViewProtocol>

 - (void)actionSheet:(SLActionSheet *)actionSheet didSelectedButtonWithButtonIndex:(NSInteger)index buttonTitle:(NSString *)buttonTitle {
    NSLog(@"actionSheet:didSelectedButtonWithButtonIndex:%ld buttonTitle:%@",index,buttonTitle);
}
 - (void)alertView:(SLAlert *)alertView didSelectedButtonWithButtonIndex:(NSInteger)index buttonTitle:(NSString *)buttonTitle {
    NSLog(@"alertView:didSelectedButtonWithButtonIndex:%ld buttonTitle:%@",index,buttonTitle);
}

转载请注明出处,好用请github star。谢谢!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,612评论 5 471
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,345评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,625评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,022评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,974评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,227评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,688评论 3 392
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,358评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,490评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,402评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,446评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,126评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,721评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,802评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,013评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,504评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,080评论 2 341

推荐阅读更多精彩内容