iOS開發(fā)通用彈窗

根據(jù)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

/*
 彈窗參數(shù) 沒有可傳nil
 @param title         標(biāo)題
 @param message       正文
 @param leftBtnTitle  左按鈕文案
 @param rightBtnTitle 又按鈕文案
 @param LeftBtnClick  左按鈕點(diǎn)擊事件回調(diào)
 @param RightBtnClick 右按鈕點(diǎn)擊事件回調(diào)
*/
- (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;
//關(guān)閉彈窗
- (void)removeView;


@end

NS_ASSUME_NONNULL_END

.m實(shí)現(xiàn)

#import "GYUniversalAlert.h"
#import <Masonry/Masonry.h>

//導(dǎo)航欄顏色
#define APP_COLOR APP_COLOR_RED

//導(dǎo)航欄顏色 0xd81e06 紅色
#define APP_COLOR_RED [UIColor colorWithRed:216/255.0 green:30/255.0 blue:6/255.0 alpha:1.0]

//rgb顏色轉(zhuǎn)換(16進(jìn)制->10進(jìn)制)
#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()
//數(shù)據(jù)
@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];
    
    //根據(jù)標(biāo)題和正文是否同時(shí)存在排版不同
    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;
    //根據(jù)標(biāo)題和正文是否同時(shí)存在排版不同
    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];
}


/*
 彈窗參數(shù) 沒有可傳nil
 @param title 標(biāo)題
 @param message 正文
 @param leftBtnTitle 左按鈕文案
 @param rightBtnTitle 又按鈕文案
 @param isCloseBtn 是否顯示底部關(guān)閉按鈕
*/
- (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:@"如果您認(rèn)為我們做的還不錯(cuò)請(qǐng)?jiān)u分鼓勵(lì)一下吧" LeftBtnTitle:@"cccc" RightBtnTitle:@"dddd" LeftBtnClick:nil RightBtnClick:^{

}];
[alert showView];
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末鹉勒,一起剝皮案震驚了整個(gè)濱河市帆锋,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌禽额,老刑警劉巖锯厢,帶你破解...
    沈念sama閱讀 216,496評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異脯倒,居然都是意外死亡实辑,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門藻丢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來徙菠,“玉大人,你說我怎么就攤上這事郁岩⌒霰迹” “怎么了?”我有些...
    開封第一講書人閱讀 162,632評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵问慎,是天一觀的道長(zhǎng)萍摊。 經(jīng)常有香客問我,道長(zhǎng)如叼,這世上最難降的妖魔是什么冰木? 我笑而不...
    開封第一講書人閱讀 58,180評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上踊沸,老公的妹妹穿的比我還像新娘歇终。我一直安慰自己,他們只是感情好逼龟,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,198評(píng)論 6 388
  • 文/花漫 我一把揭開白布评凝。 她就那樣靜靜地躺著,像睡著了一般腺律。 火紅的嫁衣襯著肌膚如雪奕短。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,165評(píng)論 1 299
  • 那天匀钧,我揣著相機(jī)與錄音翎碑,去河邊找鬼。 笑死之斯,一個(gè)胖子當(dāng)著我的面吹牛日杈,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播佑刷,決...
    沈念sama閱讀 40,052評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼达椰,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了项乒?” 一聲冷哼從身側(cè)響起啰劲,我...
    開封第一講書人閱讀 38,910評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎檀何,沒想到半個(gè)月后蝇裤,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,324評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡频鉴,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,542評(píng)論 2 332
  • 正文 我和宋清朗相戀三年栓辜,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片垛孔。...
    茶點(diǎn)故事閱讀 39,711評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡藕甩,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出周荐,到底是詐尸還是另有隱情狭莱,我是刑警寧澤,帶...
    沈念sama閱讀 35,424評(píng)論 5 343
  • 正文 年R本政府宣布概作,位于F島的核電站腋妙,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏讯榕。R本人自食惡果不足惜骤素,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,017評(píng)論 3 326
  • 文/蒙蒙 一匙睹、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧济竹,春花似錦痕檬、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至罕袋,卻和暖如春改淑,著一層夾襖步出監(jiān)牢的瞬間碍岔,已是汗流浹背浴讯。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蔼啦,地道東北人榆纽。 一個(gè)月前我還...
    沈念sama閱讀 47,722評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像捏肢,于是被迫代替她去往敵國(guó)和親奈籽。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,611評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容

  • 16宿命:用概率思維提高你的勝算 以前的我是風(fēng)險(xiǎn)厭惡者鸵赫,不喜歡去冒險(xiǎn)衣屏,但是人生放棄了冒險(xiǎn),也就放棄了無(wú)數(shù)的可能辩棒。 ...
    yichen大刀閱讀 6,046評(píng)論 0 4
  • 公元:2019年11月28日19時(shí)42分農(nóng)歷:二零一九年 十一月 初三日 戌時(shí)干支:己亥乙亥己巳甲戌當(dāng)月節(jié)氣:立冬...
    石放閱讀 6,877評(píng)論 0 2