ios 自定制弹框

自定制类似于alertView的弹框,可以根据这个方法自定制自己想要的弹框
关键点是要取到controller所在window的keyWindow, [[UIApplication sharedApplication].keyWindow addSubview:self];

调用,在需要的地方导入头文件 #import “WindowAlert.h”

WindowAlert *alert = [WindowAlert windowAlertWith:@"popup" cancle:^{
            // 点击

        }];
        [alert show];

.h文件

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
typedef void(^CancleBlock)();

@interface WindowAlert : UIView

+ (instancetype)windowAlertWith:(NSString *)bgImage cancle:(CancleBlock)cancle;
- (void)show;

@property (copy,nonatomic) CancleBlock cancleBlock;
@end

.m文件

#import "WindowAlert.h"
#import "Masonry.h"
#import "MacroDefinition.h"
@interface WindowAlert()

@property (strong,nonatomic) UIView *bgView;
@property (strong,nonatomic) UIImageView *imageView;

@end
@implementation WindowAlert

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {

        [self initBgView];
    }
    return self;
}

- (void)initBgView{

    self.bgView = [[UIView alloc]initWithFrame:self.bounds];
    [self addSubview:self.bgView];
    self.bgView.backgroundColor = COLOR_HIDDEN;

    _imageView = [UIImageView new];
    [self.bgView addSubview:_imageView];
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerX.mas_equalTo(self.bgView.bounds.origin.x).offset(0);
        make.centerY.mas_equalTo(self.bgView.bounds.origin.y).offset(0);
        make.width.mas_equalTo(280);
        make.height.mas_equalTo(265);
    }];
    _imageView.userInteractionEnabled = YES;

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_imageView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerX.mas_equalTo(_imageView.bounds.origin.x).offset(0);
        make.centerY.mas_equalTo(_imageView.bounds.origin.y).offset(90);
        make.width.mas_equalTo(95);
        make.height.mas_equalTo(38);
    }];
    [btn setBackgroundImage:[UIImage imageNamed:@"popupBtn"] forState:normal];
    [btn addTarget:self action:@selector(cancle:) forControlEvents:UIControlEventTouchUpInside];
}

+ (instancetype)windowAlertWith:(NSString *)bgImage cancle:(CancleBlock)cancle{

    WindowAlert *alert = [[WindowAlert alloc]initWithFrame:CGRectMake(0, 0,WIDTH , HEIGHT)];

    alert.imageView.image = [UIImage imageNamed:bgImage];
    alert.cancleBlock = cancle;
    return alert;
}

- (void)show{

    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

- (void)cancle:(UIButton *)btn{

    self.cancleBlock();

    [self removeFromSuperview];
}

@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值