iOS开发通用弹窗

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

推荐阅读更多精彩内容

  • 16宿命:用概率思维提高你的胜算 以前的我是风险厌恶者,不喜欢去冒险,但是人生放弃了冒险,也就放弃了无数的可能。 ...
    yichen大刀阅读 6,018评论 0 4
  • 公元:2019年11月28日19时42分农历:二零一九年 十一月 初三日 戌时干支:己亥乙亥己巳甲戌当月节气:立冬...
    石放阅读 6,864评论 0 2