iOS:一个简单的提示框

效果图:


iOS:一个简单的提示框_第1张图片

代码:.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(@"点击重新登录");

}

}];



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

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

你可能感兴趣的:(iOS:一个简单的提示框)