根据app的通用弹窗视觉封装了通用弹窗模板。
弹窗UI弹窗可以定制title文字、message文字、leftBtnTitle左按钮文案、rightBtnTitle右按钮文案、isCloseBtn底部按钮是否显示,代码如下:
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
typedef void(^leftBtnClickBlock)(void);
typedef void(^rightBtnClickBlock)(void);
/* -------------------- 拍卖通用弹窗 --------------------*/
@interface GYUniversalAlert : UIView
/*
弹窗参数 没有可传nil
@param title 标题
@param message 正文
@param leftBtnTitle 左按钮文案
@param rightBtnTitle 又按钮文案
@param LeftBtnClick 左按钮点击事件回调
@param RightBtnClick 右按钮点击事件回调
*/
- (void)alertViewWithTitle:(nullable NSString *)title LeftBtnTitle:(nullable NSString *)leftBtnTitle RightBtnTitle:(nullable NSString *)rightBtnTitle LeftBtnClick:(nullable leftBtnClickBlock)leftBtnClick RightBtnClick:(nullable rightBtnClickBlock)rightBtnClick;
- (void)alertViewWithTitle:(nullable NSString *)title Message:(nullable NSString *)message LeftBtnTitle:(nullable NSString *)leftBtnTitle RightBtnTitle:(nullable NSString *)rightBtnTitle LeftBtnClick:(nullable leftBtnClickBlock)leftBtnClick RightBtnClick:(nullable rightBtnClickBlock)rightBtnClick;
- (void)alertViewWithTitle:(nullable NSString *)title LeftBtnTitle:(nullable NSString *)leftBtnTitle RightBtnTitle:(nullable NSString *)rightBtnTitle CloseImage:(BOOL)isCloseBtn LeftBtnClick:(nullable leftBtnClickBlock)leftBtnClick RightBtnClick:(nullable rightBtnClickBlock)rightBtnClick;
- (void)alertViewWithTitle:(nullable NSString *)title Message:(nullable NSString *)message LeftBtnTitle:(nullable NSString *)leftBtnTitle RightBtnTitle:(nullable NSString *)rightBtnTitle CloseImage:(BOOL)isCloseBtn LeftBtnClick:(nullable leftBtnClickBlock)leftBtnClick RightBtnClick:(nullable rightBtnClickBlock)rightBtnClick;
//展示弹窗
- (void)showView;
//关闭弹窗
- (void)removeView;
@end
NS_ASSUME_NONNULL_END
.m实现
#import "GYUniversalAlert.h"
#import <Masonry/Masonry.h>
//导航栏颜色
#define APP_COLOR APP_COLOR_RED
//导航栏颜色 0xd81e06 红色
#define APP_COLOR_RED [UIColor colorWithRed:216/255.0 green:30/255.0 blue:6/255.0 alpha:1.0]
//rgb颜色转换(16进制->10进制)
#define ColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
@interface GYUniversalAlert()
//数据
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *message;
@property (nonatomic, strong) NSString *leftTitle;
@property (nonatomic, strong) NSString *rightTitle;
@property (nonatomic, assign) BOOL isCloseBtn;
//控件
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UIButton *rightBtn;
@property (nonatomic, strong) UIButton *closeBtn;
@property (nonatomic, copy) leftBtnClickBlock leftBlock;
@property (nonatomic, copy) rightBtnClickBlock rightBlock;
@end
@implementation GYUniversalAlert
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
#pragma mark -- UI
- (void)createSubviews{
self.backgroundColor = [UIColor clearColor];
[self addSubview:self.backView];
[self addSubview:self.contentView];
//根据标题和正文是否同时存在排版不同
if (self.title && self.title.length>0 && self.message && self.message.length > 0) {
[self.contentView addSubview:self.titleLabel];
[self.contentView addSubview:self.messageLabel];
self.titleLabel.text = self.title;
self.messageLabel.text = self.message;
}else{
[self.contentView addSubview:self.titleLabel];
self.titleLabel.font = [UIFont systemFontOfSize:36 weight:UIFontWeightMedium];
self.titleLabel.text = self.title;
}
if (self.leftTitle && self.leftTitle.length > 0) {
[self.contentView addSubview:self.leftBtn];
[self.leftBtn setTitle:self.leftTitle forState:0];
}
if (self.rightTitle && self.rightTitle.length > 0) {
[self.contentView addSubview:self.rightBtn];
[self.rightBtn setTitle:self.rightTitle forState:0];
}
if (self.isCloseBtn == YES) {
[self addSubview:self.closeBtn];
}
}
- (void)setupConstraints{
[self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
UIView *lastView = self.messageLabel;
//根据标题和正文是否同时存在排版不同
if (self.title && self.title.length>0 && self.message && self.message.length > 0) {
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.contentView).offset(25);
make.left.equalTo(self.contentView).offset(18);
make.right.equalTo(self.contentView).offset(-18);
}];
[self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.titleLabel.mas_bottom).offset(12);
make.left.equalTo(self.contentView).offset(18);
make.right.equalTo(self.contentView).offset(-18);
}];
}else{
[self.contentView addSubview:self.titleLabel];
self.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.contentView).offset(25);
make.left.equalTo(self.contentView).offset(18);
make.right.equalTo(self.contentView).offset(-18);
}];
lastView = self.titleLabel;
}
[self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(112, 44));
make.top.equalTo(lastView.mas_bottom).offset(20);
make.left.equalTo(self.contentView).offset(18);
}];
[self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(112, 44));
make.top.equalTo(self.leftBtn);
make.right.equalTo(self.contentView).offset(-18);
make.bottom.equalTo(@-18);
}];
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(@269);
make.center.equalTo(self);
}];
if (self.isCloseBtn == YES){
[self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(36, 36));
make.top.equalTo(self.contentView.mas_bottom).offset(21);
make.centerX.equalTo(self.contentView);
}];
}
}
//显示
- (void)showView{
[self createSubviews];
[self setupConstraints];
[[UIApplication sharedApplication].windows.firstObject addSubview:(UIView *)self];
}
//隐藏
- (void)removeView{
[self removeFromSuperview];
}
- (UIView *)backView{
if (!_backView) {
_backView = [[UIView alloc] init];
_backView.backgroundColor = [UIColor blackColor];
_backView.alpha = 0.5;
}
return _backView;
}
- (UIView *)contentView{
if (!_contentView) {
_contentView = [[UIView alloc] init];
_contentView.backgroundColor = [UIColor whiteColor];
_contentView.layer.cornerRadius = 18;
_contentView.layer.masksToBounds = YES;
}
return _contentView;
}
- (UILabel *)titleLabel{
if (!_titleLabel) {
_titleLabel = [UILabel new];
_titleLabel.font = [UIFont systemFontOfSize:21 weight:UIFontWeightMedium];
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.textColor = ColorFromRGB(0x111111);
_titleLabel.numberOfLines = 0;
}
return _titleLabel;
}
- (UILabel *)messageLabel{
if (!_messageLabel) {
_messageLabel = [UILabel new];
_messageLabel.font = [UIFont systemFontOfSize:14];
_messageLabel.textAlignment = NSTextAlignmentCenter;
_messageLabel.textColor = ColorFromRGB(0x666666);
_messageLabel.numberOfLines = 0;
}
return _messageLabel;
}
- (UIButton *)leftBtn {
if (!_leftBtn) {
_leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_leftBtn setBackgroundColor:[UIColor whiteColor]];
[_leftBtn setTitleColor:ColorFromRGB(0x999999) forState:UIControlStateNormal];
_leftBtn.layer.borderColor = ColorFromRGB(0xCCCCCC).CGColor;
_leftBtn.layer.borderWidth = 0.5;
[_leftBtn setTitle:@"取消" forState:UIControlStateNormal];
_leftBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:18];
[_leftBtn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
_leftBtn.layer.cornerRadius = 22;
_leftBtn.layer.masksToBounds = YES;
}
return _leftBtn;
}
- (UIButton *)rightBtn {
if (!_rightBtn) {
_rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_rightBtn.layer.cornerRadius = 22;
_rightBtn.layer.masksToBounds = YES;
_rightBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:18];
[_rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_rightBtn setBackgroundColor:APP_COLOR];
[_rightBtn setTitle:@"确定" forState:UIControlStateNormal];
[_rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
}
return _rightBtn;
}
- (UIButton *)closeBtn{
if (!_closeBtn) {
_closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_closeBtn setBackgroundImage:[UIImage imageNamed:@"pm_mk_coupon_close@3x.png"] forState:0];
[_closeBtn addTarget:self action:@selector(closeAlert) forControlEvents:UIControlEventTouchUpInside];
}
return _closeBtn;
}
- (void)leftBtnClick{
if (self.leftBlock) {
self.leftBlock();
}
[self removeView];
}
- (void)rightBtnClick{
if (self.rightBlock) {
self.rightBlock();
}
[self removeView];
}
- (void)closeAlert{
[self removeView];
}
/*
弹窗参数 没有可传nil
@param title 标题
@param message 正文
@param leftBtnTitle 左按钮文案
@param rightBtnTitle 又按钮文案
@param isCloseBtn 是否显示底部关闭按钮
*/
- (void)alertViewWithTitle:(nullable NSString *)title LeftBtnTitle:(nullable NSString *)leftBtnTitle RightBtnTitle:(nullable NSString *)rightBtnTitle LeftBtnClick:(nullable leftBtnClickBlock)leftBtnClick RightBtnClick:(nullable rightBtnClickBlock)rightBtnClick{
[self alertViewWithTitle:title Message:nil LeftBtnTitle:leftBtnTitle RightBtnTitle:rightBtnTitle CloseImage:NO LeftBtnClick:leftBtnClick RightBtnClick:rightBtnClick];
}
- (void)alertViewWithTitle:(nullable NSString *)title Message:(nullable NSString *)message LeftBtnTitle:(nullable NSString *)leftBtnTitle RightBtnTitle:(nullable NSString *)rightBtnTitle LeftBtnClick:(nullable leftBtnClickBlock)leftBtnClick RightBtnClick:(nullable rightBtnClickBlock)rightBtnClick{
[self alertViewWithTitle:title Message:message LeftBtnTitle:leftBtnTitle RightBtnTitle:rightBtnTitle CloseImage:NO LeftBtnClick:leftBtnClick RightBtnClick:rightBtnClick];
}
- (void)alertViewWithTitle:(nullable NSString *)title LeftBtnTitle:(nullable NSString *)leftBtnTitle RightBtnTitle:(nullable NSString *)rightBtnTitle CloseImage:(BOOL)isCloseBtn LeftBtnClick:(nullable leftBtnClickBlock)leftBtnClick RightBtnClick:(nullable rightBtnClickBlock)rightBtnClick{
[self alertViewWithTitle:title Message:nil LeftBtnTitle:leftBtnTitle RightBtnTitle:rightBtnTitle CloseImage:isCloseBtn LeftBtnClick:leftBtnClick RightBtnClick:rightBtnClick];
}
- (void)alertViewWithTitle:(nullable NSString *)title Message:(nullable NSString *)message LeftBtnTitle:(nullable NSString *)leftBtnTitle RightBtnTitle:(nullable NSString *)rightBtnTitle CloseImage:(BOOL)isCloseBtn LeftBtnClick:(nullable leftBtnClickBlock)leftBtnClick RightBtnClick:(nullable rightBtnClickBlock)rightBtnClick{
self.title = title;
self.message = message;
self.leftTitle = leftBtnTitle?leftBtnTitle:@"取消";
self.rightTitle = rightBtnTitle?rightBtnTitle:@"确定";
self.isCloseBtn = isCloseBtn?isCloseBtn:NO;
self.leftBlock = leftBtnClick;
self.rightBlock = rightBtnClick;
}
@end
使用距举例
GYUniversalAlert *alert = [[GYUniversalAlert alloc] initWithFrame:CGRectMake(0, 0, 375, 667)];
[alert alertViewWithTitle:@"如果您认为我们做的还不错请评分鼓励一下吧" LeftBtnTitle:@"cccc" RightBtnTitle:@"dddd" LeftBtnClick:nil RightBtnClick:^{
}];
[alert showView];
网友评论