iOS:一个简单的提示框

效果图:


代码:.h中

typedef void(^SBBlock)(NSInteger num);

@interface SBAlertView : UIView

/**

*  蒙版

*/

@property (nonatomic,strong)UIView *blackView;

/**

*  提示框

*/

@property (strong,nonatomic)UIView * alertview;

/**

*  标题

*/

@property (strong,nonatomic)NSString * title;

/**

*  内容

*/

@property (nonatomic,copy)NSString *contentStr;

-(void)initWithTitle:(NSString *)title contentStr:(NSString *)content BtnArray:(NSArray *)array andBlock:(SBBlock)block;


--------------------------------分割线--------------------------------

.m中


@interface SBAlertView ()

/**

*  创建block对象

*/

@property (nonatomic, copy) SBBlock block;

/**

*  标题label

*/

@property(strong,nonatomic)UILabel *isCreate;

/**

*  内容

*/

@property(strong,nonatomic)UILabel *attenL;

/**

*  取消按钮

*/

@property(strong,nonatomic)UIButton *btn1;

/**

*  确定按钮

*/

@property(strong,nonatomic)UIButton *btn2;

/**

*  横线

*/

@property(strong,nonatomic)UILabel *label1;

/**

*  竖线

*/

@property(strong,nonatomic)UILabel *label2;

@end

@implementation SBAlertView

- (instancetype)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

//创建遮罩

self.blackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];

self.blackView.backgroundColor = [UIColor blackColor];

self.blackView.alpha = 0.5;

UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];

tap1.cancelsTouchesInView = NO;

[self.blackView addGestureRecognizer:tap1];

[self addSubview:_blackView];

//创建alert

self.alertview = [[UIView alloc]initWithFrame:CGRectMake(30, HEIGHT/2-120-64, WIDTH-60, 240)];

//        self.alertview.center = self.center;

self.alertview.layer.cornerRadius = 5;

self.alertview.clipsToBounds = YES;

self.alertview.backgroundColor = [UIColor whiteColor];

[self addSubview:_alertview];

[self createAliertView];

}

return self;

}

-(void)viewTapped:(UITapGestureRecognizer*)tap1

{

self.alpha = 0.0;

[self removeFromSuperview];

self.alertview = nil;

}

//创建View

-(void)createAliertView{

self.isCreate = [[UILabel alloc]initWithFrame:CGRectMake( 20,10, self.alertview.frame.size.width-40, 43)];

self.isCreate.font = [UIFont boldSystemFontOfSize:18];

self.isCreate.textAlignment=NSTextAlignmentCenter;

self.attenL = [[UILabel alloc]initWithFrame:CGRectMake(self.isCreate.frame.origin.x, self.isCreate.frame.origin.y+20, self.alertview.frame.size.width-40, 100)];

self.attenL.font = [UIFont systemFontOfSize:15];

self.attenL.numberOfLines = 0;

self.attenL.font = [UIFont systemFontOfSize:14];

self.attenL.textColor = [UIColor blackColor];

[self.alertview addSubview:self.attenL];

[self.alertview addSubview:self.isCreate];

[self createBtnTitleWithCancel];

}

//创建button

- (void)createBtnTitleWithCancel

{

CGFloat m = self.alertview.frame.size.width;

//横线和竖线

self.label1=[[UILabel alloc] initWithFrame:CGRectMake(0, self.alertview.height-50, self.alertview.width, 1)];

self.label1.backgroundColor=[UIColor lightGrayColor];

[self.alertview addSubview:self.label1];

self.label2=[[UILabel alloc] initWithFrame:CGRectMake(self.alertview.width/2, self.alertview.height-50, 1, 50)];

self.label2.backgroundColor=[UIColor lightGrayColor];

[self.alertview addSubview:self.label2];

//取消按钮

self.btn1 = [UIButton buttonWithType:UIButtonTypeCustom];

self.btn1.tag=1001;

self.btn1.frame = CGRectMake(0, self.alertview.height-50,m/2, 50);

[self.btn1 setTitleColor:TINTCOLOR forState:UIControlStateNormal];

[self.btn1 addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];

[self.btn1.titleLabel setFont:[UIFont systemFontOfSize:18]];

//确定按钮

self.btn2 = [UIButton buttonWithType:UIButtonTypeCustom];

self.btn2.tag=1002;

self.btn2.frame = CGRectMake(m/2, self.alertview.height-50,m/2, 50);

[self.btn2 setTitleColor:TINTCOLOR forState:UIControlStateNormal];

[self.btn2 addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];

[self.btn2.titleLabel setFont:[UIFont systemFontOfSize:18]];

[self.alertview addSubview:self.btn1];

[self.alertview addSubview:self.btn2];

}

- (void)clickBtn:(UIButton *)btn

{

self.block(btn.tag-1000);

[self.blackView removeFromSuperview];

}

//传值,并且高度自适应

-(void)initWithTitle:(NSString *)title contentStr:(NSString *)content BtnArray:(NSArray *)array andBlock:(SBBlock)block

{

self.block=block;

CGFloat m = self.alertview.frame.size.width;

self.isCreate.text = title;

self.attenL.text= content;

if (array.count==1) {

[self.btn2 setTitle:[NSString stringWithFormat:@"%@", array[0]] forState:UIControlStateNormal];

}else{

[self.btn1 setTitle:[NSString stringWithFormat:@"%@", array[0]] forState:UIControlStateNormal];

[self.btn2 setTitle:[NSString stringWithFormat:@"%@", array[1]] forState:UIControlStateNormal];

}

CGRect r = [content boundingRectWithSize:CGSizeMake(self.alertview.width-40,1000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.f]} context:nil];

self.attenL.frame = CGRectMake(self.isCreate.frame.origin.x, CGRectGetMaxY(self.isCreate.frame), self.alertview.frame.size.width-40, r.size.height);

if (array.count==1) {

self.btn2.frame=CGRectMake(0, CGRectGetMaxY(self.attenL.frame)+15, m, 50);

[self.btn1 removeFromSuperview];

[self.label2 removeFromSuperview];

}else{

self.btn1.frame=CGRectMake(0, CGRectGetMaxY(self.attenL.frame)+15, m/2, 50);

self.btn2.frame=CGRectMake(m/2, CGRectGetMaxY(self.attenL.frame)+15,m/2, 50);

}

self.label1.frame=CGRectMake(0, CGRectGetMaxY(self.attenL.frame)+15, self.alertview.width, 1);

self.label2.frame=CGRectMake(self.alertview.width/2,CGRectGetMaxY(self.attenL.frame)+15, 1, 50);

self.alertview.frame=CGRectMake(30, HEIGHT/2-120-64,WIDTH-60 ,CGRectGetMaxY(self.btn2.frame)+1);

}

--------------------------------分割线--------------------------------

使用:


SBAlertView *alertView = [[SBAlertView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];

NSArray *array=@[@"退出",@"重新登录"];//也可以只写一个,看需要

[alertView initWithTitle:@"下线通知" contentStr:@"你的账号在另一台设备登录。如非本人操作,则密码可能已泄露,建议立即修改密码或冻结账号。" BtnArray:array andBlock:^(NSInteger num){

if (num == 1)

{

// 退出

NSLog(@"点击退出");

}

else

{

NSLog(@"点击重新登录");

}

}];



写在后面:新手初试,不喜勿喷。感谢~(喜欢点❤️)

有不对的地方请告诉我,一起进步。再次感谢!

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

推荐阅读更多精彩内容