模仿系统方法自定义UIAlertView的实现和代理

简介: 模仿系统方法自定义UIAlertView的实现和代理

背景是博主偷懒依然用了iOS8后被废弃掉的UIAlertView,然后取消的字体比确定要粗,同时为了统一界面风格,需要把UIAlertView统一换成定制的样子,博主就犯难了,又不想大批量更改代码,怎么办,突然意识到为什么我不能自己写一个类似系统的方法呢,只要整体代码一样,不就可以通过搜索统一修改一下类名就好了么?说干就干,这里贴上代码:

#import <UIKit/UIKit.h>
@class MyAlertView;
@protocol MyAlertDelegate <NSObject>
-(void)alertView:(MyAlertView *)alertView clickedButtonAtIndex:(NSUInteger)buttonIndex;
@end
@interface MyAlertView : UIView
@property (weak,nonatomic) id<MyAlertDelegate>delegate;
-(instancetype)initWithTitle:title message:messge delegate:delegate cancelButtonTitle:cancelTitle otherButtonTitle:otherTitle;
-(void)show;
@end
#import "MyAlertView.h"
#define WIDTH [UIApplication sharedApplication].keyWindow.frame.size.width
#define HEIGHT [UIApplication sharedApplication].keyWindow.frame.size.height
@interface MyAlertView()
@property(nonatomic,strong)UIView *blackView;
@property(nonatomic,strong)UIView *alertBackView;
@property(nonatomic,strong)UILabel *contentLabel;
@property(nonatomic,strong)UIButton *cancelBtn;
@property(nonatomic,strong)UIButton *okBtn;
@property(nonnull,strong)NSString *title;
@property(nonnull,strong)NSString *content;
@property(nonnull,strong)NSString *cancelBtnTitle;
@property(nonnull,strong)NSString *okBtnTitle;
@end
@implementation MyAlertView
-(instancetype)initWithTitle:title message:messge delegate:delegate cancelButtonTitle:cancelTitle otherButtonTitle:otherTitle
{
    if (self=[super initWithFrame:[[UIApplication sharedApplication] keyWindow].frame]) {
        _delegate=delegate;
        _title=title;
        _content=messge;
        _cancelBtnTitle=cancelTitle;
        _okBtnTitle=otherTitle;
        [self setUp];
    }
    return self;
}
-(void)setUp
{
    NSLog(@"%f",WIDTH);
    NSLog(@"%f",HEIGHT);
    //黑色背景
    _blackView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    _blackView.backgroundColor=[UIColor blackColor];
    _blackView.alpha=0.6;
    [self addSubview:_blackView];
    //弹出框背景
    _alertBackView=[[UIView alloc]initWithFrame:CGRectMake(20, 44, WIDTH-40, 160)];
    _alertBackView.layer.cornerRadius=6;
    _alertBackView.center=CGPointMake(320/2, 568/2);
    _alertBackView.backgroundColor=[UIColor whiteColor];
    [self addSubview:_alertBackView];
    //标题
    UILabel *alertTitle=[[UILabel alloc]initWithFrame:CGRectMake(20, 10, WIDTH-40-40, 20)];
    alertTitle.text=_title;
    alertTitle.textAlignment=NSTextAlignmentCenter;
    alertTitle.textColor=[UIColor colorWithRed:0.31f green:0.31f blue:0.31f alpha:1.00f];
    alertTitle.font=[UIFont systemFontOfSize:15];
    [_alertBackView addSubview:alertTitle];
    //消息内容
    UILabel *message=[[UILabel alloc]initWithFrame:CGRectMake(20, 30, WIDTH-40-40, 60)];
    message.text=_content;
    message.numberOfLines=0;
    message.textAlignment=NSTextAlignmentCenter;
    message.textColor=[UIColor colorWithRed:0.31f green:0.31f blue:0.31f alpha:1.00f];
    message.font=[UIFont systemFontOfSize:15];
    [_alertBackView addSubview:message];
    //    CAKeyframeAnimation * animation;
    //    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    //    animation.duration = 0.5;
    //    animation.delegate = self;
    //    animation.removedOnCompletion = YES;
    //    animation.fillMode = kCAFillModeForwards;
    //
    //    NSMutableArray *values = [NSMutableArray array];
    //    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    //    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.0)]];
    //    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]];
    //    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    //
    //    animation.values = values;
    //    [_alertBackView.layer addAnimation:animation forKey:nil];
    //按钮背景
    UIView *btnBGView=[[UIView alloc]initWithFrame:CGRectMake(0, 100, WIDTH-40, 60)];
    btnBGView.backgroundColor=[UIColor colorWithRed:0.96f green:0.96f blue:0.96f alpha:1.00f];
    [_alertBackView addSubview:btnBGView];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:btnBGView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(3, 3)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = btnBGView.bounds;
    maskLayer.path = maskPath.CGPath;
    btnBGView.layer.mask = maskLayer;
    if (_cancelBtnTitle==nil||_okBtnTitle==nil) {
        //确定按钮
        _okBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        _okBtn.frame=CGRectMake((WIDTH-40-220)/2, 15, 220, 30);
        _okBtn.titleLabel.font=[UIFont systemFontOfSize:15];
        [_okBtn setTitle:_cancelBtnTitle==nil ? _okBtnTitle:_cancelBtnTitle forState:UIControlStateNormal];
        [_okBtn setBackgroundImage:[UIImage imageNamed:@"login_loginbtn.png"] forState:UIControlStateNormal];
        [_okBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _okBtn.tag=1;
        [_okBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [btnBGView addSubview:_okBtn];
    }
    else
    {
        //取消按钮
        _cancelBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        _cancelBtn.frame=CGRectMake(25, 15,100, 30);
        _cancelBtn.titleLabel.font=[UIFont systemFontOfSize:15];
        [_cancelBtn setTitle:_cancelBtnTitle forState:UIControlStateNormal];
        [_cancelBtn setBackgroundImage:[UIImage imageNamed:@"detail_alert_left"] forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor colorWithRed:0.47f green:0.47f blue:0.47f alpha:1.00f] forState:UIControlStateNormal];
        _cancelBtn.tag=0;
        [_cancelBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [btnBGView addSubview:_cancelBtn];
        //确定按钮
        _okBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        _okBtn.frame=CGRectMake(WIDTH-40-25-100, 15, 100, 30);
        _okBtn.titleLabel.font=[UIFont systemFontOfSize:15];
        [_okBtn setTitle:_okBtnTitle forState:UIControlStateNormal];
        [_okBtn setBackgroundImage:[UIImage imageNamed:@"detail_alert_right"] forState:UIControlStateNormal];
        [_okBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _okBtn.tag=1;
        [_okBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [btnBGView addSubview:_okBtn];
    }
}
-(void)buttonAction:(UIButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)]) {
        [self.delegate alertView:self clickedButtonAtIndex:(btn.tag)];
    }
    self.alpha = 0.0;
    [self removeFromSuperview];
    _blackView=nil;
    _alertBackView=nil;
}
-(void)show
{
    UIView * keywindow = [[UIApplication sharedApplication] keyWindow];
    [keywindow addSubview:self];
}

原谅我没写很多注释,不过我觉得大家都能看得懂,我并没有写的很复杂。这里需要注意的问题是,怎么把自己的alert添加到控制器上来显示,添加是简单,可是像系统那样,不在调用时设置frame和利用控制器addSubView怎么实现呢?博主一开始纠结了好久,不过最后算是写出来了,参考的小伙伴要认真看下,还有代理的实现步骤,需要格外注意下哦,代码中注释部分为动画效果,自定义alert弹出时不会像系统的那样直接让当前键盘收回,如果有键盘时触发,键盘有可能会遮挡到当前alert,需要在触发时手动对键盘resignFirstResponder。其他的博主就不详细说明了,代码中有体现。附上github下载地址:https://github.com/codeliu6572/CustomAlertView

目录
相关文章
|
网络协议
Wireshark 捕获和显示过滤器
Wireshark 捕获和显示过滤器
1082 0
|
11月前
|
弹性计算 人工智能 机器人
Moltbot部署又出新玩法!阿里云计算巢全流程方案上线
昨日,阿里云推出「轻量服务器×Moltbot」全流程部署方案,已打通千问、钉钉、imessage。今天,阿里云继续迭代,为用户带来更多便捷的部署方式——【计算巢×Moltbot全流程部署方案】也火速上线!相较于轻量应用服务器,计算巢部署有以下区别,用户可以根据自己的需求进行选择。
|
10月前
|
人工智能 安全 数据安全/隐私保护
2026年阿里云部署OpenClaw(Clawdbot)一键接入iMessage喂饭级教程
2026年,OpenClaw(前身为Clawdbot、Moltbot)凭借轻量化容器化架构、灵活的跨平台集成能力,成为个人与小型团队打造专属AI助手的首选工具,其支持自然语言指令解析、多任务自动化执行、多终端同步响应的核心特性,完美适配苹果生态用户的办公与生活需求。iMessage作为苹果生态核心的即时通讯工具,覆盖iPhone、Mac、iPad全终端,具备端到端加密、无缝同步的优势,将OpenClaw与iMessage深度对接,可实现“iMessage发指令、AI自动化执行、全终端收结果”的跨场景体验,无需切换APP,随时随地调用AI能力处理文档生成、数据查询、任务提醒等事务。
855 1
|
机器学习/深度学习 存储 算法
Trinity-RFT:构建智能体持续学习的自动化强化微调工厂
大型语言模型作为智能体在真实环境中持续交互学习面临诸多挑战。 Trinity-RFT 是通义实验室推出的强化微调框架,旨在实现智能体的持续进化。它通过探索、训练与经验池的解耦设计,支持多样化训练模式,提升资源利用率和学习稳定性。同时,Trinity-RFT 提供灵活的数据处理与算法模块化功能,降低应用与研究门槛,助力迈向终身学习与自主进化的智能体时代。
1316 2
|
存储 数据安全/隐私保护 iOS开发
OFDM SYMBOL的概念及其速率
OFDM SYMBOL的概念及其速率
1017 1
|
监控
交换机中接收数据帧
【10月更文挑战第1天】
541 1
|
分布式计算 监控 数据挖掘
MaxCompute的应用
【5月更文挑战第7天】MaxCompute的应用
395 8
|
存储 分布式计算 算法
面试题:海量数据去重、Top-k、BitMap问题整理
首先直接进入正题,40亿QQ号如何设计算法去重,相同的QQ号码仅保留一个,内存限制为1个G。 (腾讯的QQ号都是4字节正整数,所以QQ号码的个数是43亿左右,理论值2^32-1个,又因为是无符号的,翻倍了一下,所以43亿左右)
面试题:海量数据去重、Top-k、BitMap问题整理
|
网络协议 安全 Oracle
Java网络编程超详细笔记
本文主要分享了在Java的网络编程,首先简单的说了两种网络模型以及网络编程的三要素(IP地址、端口号、协议),之后分享了InetAddress类(获取ip、主机名称的方法),重点是UDP和TCP的传输,并分享了若干案例(客户端键盘录入服务端输出文件去案例、服务端给出反馈案例、传输图片案例等等);
481 1