iOS開發通用彈窗

根據app的通用彈窗視覺封裝了通用彈窗模板。

彈窗可以定製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:@"[email protected]"] 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];
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章