1 ,声明变量
/* 遮罩window */
@property(nonatomic,strong)UIWindow *coverWindow;
/* 弹框 */
@property(nonatomic,strong)UIView *alertView;
/* 弹框按钮 */
@property(nonatomic,strong)UIButton *nextBtn;
/* 弹框标题 */
@property(nonatomic,strong)UILabel *titleLab;
//kUIScreenHeight 屏幕高度
2 , 根据字数设置高度
#pragma mark 弹出公告提示
-(UIView *)alertView
{
NSString *textStr = @"系统将于2017年4月20日17~19点间进行升级,在升级期间系统将停止使用,请你提前处理系统中待处理任务。";
if (!_alertView) {
CGRect rect = [textStr boundingRectWithSize:CGSizeMake(kUIScreenWidth -100, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:kPingFang_Regular size:14]} context:nil];
float alertHight = rect.size.height + 55 +85;
//高度小于230 就固定高度为230
//高度大于 屏幕的高度-130 固定高度 屏幕高度-130
if (alertHight <= 230) {
alertHight = 230;
} else if (alertHight >= kUIScreenHeight-130) {
alertHight = kUIScreenHeight-130;
}else {
alertHight = rect.size.height +40+ 40 +15 +70+15;
}
_alertView = [[UIView alloc]initWithFrame:CGRectMake(30, (kUIScreenHeight-alertHight)/2, kUIScreenWidth-60, alertHight)];
_alertView.backgroundColor = [UIColor whiteColor];
_coverWindow = [[UIWindow alloc]initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight)];
_coverWindow.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
_alertView.clipsToBounds = YES;
_alertView.layer.cornerRadius = 4;
[_coverWindow addSubview:_alertView];
_coverWindow.windowLevel = UIWindowLevelNormal;
_coverWindow.hidden = YES;
[[UIApplication sharedApplication].keyWindow addSubview:_coverWindow];
//右上角 X 按钮
UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
deleteBtn.backgroundColor = [CommonFunctions colorWithHex:0xffffff];
[deleteBtn setFrame:CGRectMake(_alertView.frame.size.width-31, 15, 16, 16)];
[deleteBtn setBackgroundImage:[UIImage imageNamed:@"btn_cls"] forState:UIControlStateNormal];
[deleteBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[_alertView addSubview:deleteBtn];
//下一步的按钮
UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
nextBtn.backgroundColor = [CommonFunctions colorWithHex:0x11a1b4];
[nextBtn setFrame:CGRectMake(30, _alertView.frame.size.height-70, _alertView.frame.size.width-60, 40)];
[nextBtn setTitle:buttonText forState:UIControlStateNormal];
[nextBtn.titleLabel setFont:[UIFont fontWithName:kPingFang_Regular size:16]];
[nextBtn addTarget:self action:@selector(nextBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[_alertView addSubview:nextBtn];
//标题 Lab
UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, _alertView.frame.size.width-20, 20)];
titleLab.text = titleStr;
titleLab.textColor = [CommonFunctions colorWithHex:0x666666];
titleLab.font = [UIFont fontWithName:kPingFang_Regular size:16];
titleLab.textAlignment = NSTextAlignmentCenter;
[_alertView addSubview:titleLab];
//显示内容的textView (也可以用lab)
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(20, 40+15, _alertView.frame.size.width-40, alertHight-40-15-70-15)];
textView.text = textStr;
textView.textColor = [CommonFunctions colorWithHex:0x999999];
textView.font = [UIFont fontWithName:kPingFang_Regular size:14];
textView.editable = NO;
textView.bounces = NO;
[_alertView addSubview:textView];
}
return _alertView;
}
3 , 调用 显示和隐藏
//显示
[self alertView];
self.alertView.hidden = NO;
self.coverWindow.hidden = NO;
//隐藏
self.alertView.hidden = YES;
self.coverWindow.hidden = YES;
4 ,实现按钮的方法 就OK了