自定义同时有图片文字的提示框

来源:互联网 发布:愚人节整人软件 编辑:程序博客网 时间:2024/05/26 19:15

废话不多说,代码都能看懂,

.h文件

//

//  XJAlertView.h

//  XiaoQiao

//

//  Created by 王小胜 on 2017/6/20.

//  Copyright © 2017 Ningbo Xiaojiang IOT Technology Co., LTD. All rights reserved.

//


#import <UIKit/UIKit.h>


typedef void(^alertViewSureBlock)();

@interface XJAlertView : UIView


@property(nonatomic,copy)alertViewSureBlock block;


+ (instancetype)sharedAlertView;


//快速显示

- (void)alertViewMessage :(NSString *)message cancelTitle:(NSString *)cancelTitle sureTitle:(NSString *)sureTitle image:(NSString *)image Block:(alertViewSureBlock)block;


@end


.m文件

//

//  XJAlertView.m

//  XiaoQiao

//

//  Created by 王小胜 on 2017/6/20.

//  Copyright © 2017 Ningbo Xiaojiang IOT Technology Co., LTD. All rights reserved.

//


#import "XJAlertView.h"

#import "Masonry.h"


#define kColor(R,G,B) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1]


@interface XJAlertView (){

    

    NSString * sureTitles;

    NSString * cancelTitles;

    NSString * messages;

    NSString * titleimage;

}


@property(nonatomic,strong)UIButton *canBtn;

@property(nonatomic,strong)UIButton *sureBtn;

@property(nonatomic,strong)UILabel *messageLable;

@property(nonatomic,strong)UIImageView * images;

@property(nonatomic,strong)UIView *showView;

@property(nonatomic,strong)UIView *levelView;

@property(nonatomic,strong)UIView *verticalView;


@end


@implementation XJAlertView


static id _alertView =nil;


+ (instancetype)sharedAlertView{

    

    return  [[selfalloc]init];;

}

- (instancetype)init{

    if (self == [superinit]) {

        

        [selfsetUpframe];

        

    }

    returnself;

}


- (void)layoutSubviews{

    [superlayoutSubviews];

    

    if (cancelTitles !=nil) {

        [self.canBtnmas_makeConstraints:^(MASConstraintMaker *make) {

            make.width.mas_equalTo(self.frame.size.width/2);

            make.height.mas_equalTo(60);

            make.bottom.left.mas_equalTo(self);

        }];

        

    }

    

    if (sureTitles !=nil) {

        [self.sureBtnmas_makeConstraints:^(MASConstraintMaker *make) {

            make.width.mas_equalTo(self.frame.size.width/2);

            make.height.mas_equalTo(60);

            make.bottom.right.mas_equalTo(self);

        }];

    }

    

    

    

    if (titleimage !=nil) {

        [self.imagesmas_makeConstraints:^(MASConstraintMaker *make) {

            make.width.mas_equalTo(120);

            make.height.mas_equalTo(120);

            make.top.mas_equalTo(self);

            make.centerX.equalTo(self);

            

        }];

    }

    

    if (messages !=nil) {

        [self.messageLablemas_makeConstraints:^(MASConstraintMaker *make) {

            make.height.mas_equalTo(30);

            make.left.mas_equalTo(self).offset(5);

            make.right.mas_equalTo(self).offset(-5);

            make.top.mas_equalTo(self).offset(125);

        }];

    }

    

    [self.levelViewmas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(self);

        make.right.mas_equalTo(self);

        make.height.mas_equalTo(1);

        make.top.mas_equalTo(self.frame.size.height -60);

    }];

    

    [self.verticalViewmas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(self.frame.size.width/2-1);

        make.width.mas_equalTo(1);

        make.bottom.mas_equalTo(self);

        make.top.mas_equalTo(self.frame.size.height -60);

    }];

    

}

- (void)setUpframe{

    

    self.showView = [[UIViewalloc]initWithFrame:[UIScreenmainScreen].bounds];

    self.showView.backgroundColor = [UIColorcolorWithRed:0green:0blue:0alpha:0.5];

    [[UIApplicationsharedApplication].keyWindowaddSubview:self.showView];

    [self.showViewaddSubview:self];

    

    self.backgroundColor = [UIColorwhiteColor];

    self.layer.cornerRadius =5;

    self.layer.masksToBounds =YES;

    

    self.backgroundColor = [UIColorwhiteColor];

    self.frame =CGRectMake(0,0,280,240);

    self.center =CGPointMake([UIScreenmainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2);

    

    _canBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

    _canBtn.titleLabel.font = [UIFontsystemFontOfSize:19.0f];

    [_canBtnsetTitleColor:kColor(165,166,167) forState:UIControlStateNormal];

    [_canBtnaddTarget:selfaction:@selector(cancelTitle)forControlEvents:UIControlEventTouchUpInside];

    

    [selfaddSubview:_canBtn];

    

    _sureBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

    _sureBtn.titleLabel.font = [UIFontsystemFontOfSize:19.0f];

    [_sureBtnsetTitleColor:kColor(81,128,217) forState:UIControlStateNormal];

    [_sureBtnaddTarget:selfaction:@selector(sureTitle)forControlEvents:UIControlEventTouchUpInside];

    [selfaddSubview:_sureBtn];

    

    _messageLable = [[UILabelalloc]init];

    _messageLable.textAlignment =NSTextAlignmentCenter;

    _messageLable.font = [UIFontsystemFontOfSize:16.0f];

    _messageLable.numberOfLines =0;

    [selfaddSubview:_messageLable];

    

    _images = [[UIImageViewalloc]init];

    [selfaddSubview:_images];

    

    _levelView = [[UIViewalloc]init];

    _levelView.backgroundColor =kColor(231,232,233);

    [selfaddSubview:_levelView];

    

    _verticalView = [[UIViewalloc]init];

    _verticalView.backgroundColor =kColor(231,232,233);

    [selfaddSubview:_verticalView];

    

    

}


- (void)alertViewMessage:(NSString *)message cancelTitle:(NSString *)cancelTitle sureTitle:(NSString *)sureTitle image:(NSString *)image Block:(alertViewSureBlock)block{

    

    

    cancelTitles = cancelTitle;

    sureTitles = sureTitle;

    titleimage = image;

    messages = message;

    self.block = block;

    

    

    [_canBtnsetTitle:cancelTitlesforState:UIControlStateNormal];

    [_sureBtnsetTitle:sureTitlesforState:UIControlStateNormal];

    _messageLable.text =messages;

    _images.image = [UIImageimageNamed:titleimage];

}

- (void)cancelTitle{

    

    [self.showViewremoveFromSuperview];

}

- (void)sureTitle{

    self.block();

    [self.showViewremoveFromSuperview];

}


@end


在需要用的地方导入头文件然后实例化对象:

XJAlertView * alert = [XJAlertViewsharedAlertView];

    [alert alertViewMessage:@"欢迎使用,这是一个自定义的弹出框,你可以加上其它功能,这只是一个测试!"cancelTitle:@"取消"sureTitle:@"确定"image:@"run_icon"Block:^{

        NSLog(@"123");

    }];