这个是和UIAlertView类似,但是可以自定义view的样式废话不多说,上代码:

首先第一步:创建一个继承自View的类如:

#import <UIKit/UIKit.h>

@class <#你自己的类名#>;

@protocol PopupViewDelegate <NSObject>

- (void)popupView:(<#你自己的类名#>*)popupView ClickedButtonAtIndex:(NSInteger)buttonIndex;

@end

@interface <#你自己的类名#> : UIView

/**初始化View

* title   * 提示内容

* message * 需要显示的内容

* cancelButtonTitle *取消按钮的文字

* okButtonTitles *确认按钮的文字

* delegate  * 当前代理者

*/

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitles:(NSString *)okButtonTitles delegate:(id)delegate;

/**弹出该View*/

- (void)show;

@property (nonatomic,strong)id <PopupViewDelegate>  delegate;

@end

/**上方的方法直接贴到View的.h文件中就好,当然类名需要更改的*/

//屏幕宽度

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

//获得屏幕高度

#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

#define kNoButtonW 120

#define kNoButtonH 35

#define kButtonRadius 15

#define kFontNameStrBold @"Helvetica-Bold"

#define kFontNameStrRegular @"Helvetica"

#import " <#你自己的类名#>"

@interface  <#你自己的类名#>()

/**中间黑色的View*/

@property (nonatomic,strong)UIView * blackeView;

/**提示框的Lable*/

@property (nonatomic,strong)UILabel * titleLable;

/**描述内容的Lable*/

@property (nonatomic,strong)UILabel * desLabel;

/**取消或者NO的button*/

@property (nonatomic,strong)UIButton * noButton;

/**YES或者OK的button*/

@property (nonatomic,strong)UIButton * yesButton;

@end

@implementation  <#你自己的类名#>

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitles:(NSString *)okButtonTitles delegate:(id)delegate

{

if (self = [super init]) {

self.frame = CGRectMake(0, 0,SCREEN_WIDTH, SCREEN_HEIGHT);

self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];

/**设置弹出View的动画*/

[self setupWithBlackView];

/**往黑色View中添加子控件*/

[self setupWithAddControls];

self.titleLable.text = title;

self.desLabel.text = message;

self.delegate = delegate;

[self.noButton setTitle:cancelButtonTitle forState:UIControlStateNormal];

[self.yesButton setTitle:okButtonTitles forState:UIControlStateNormal];

}

return self;

}

/**设置添加黑色View的动画*/

-(void)setupWithBlackView

{

UIView * blackView =[[UIView alloc]init];

blackView.layer.cornerRadius = 10;

blackView.layer.masksToBounds = YES;

self.blackeView = blackView;

blackView.backgroundColor = [UIColor colorWithRed:31/255.0 green:31/255.0 blue:31/255.0 alpha:1];

[self addSubview:blackView];

}

/**往黑色View中添加子控件*/

-(void)setupWithAddControls

{

/**标题文本*/

UILabel * titleLable = [[UILabel alloc]init];

titleLable.numberOfLines = 0;

titleLable.textAlignment = NSTextAlignmentCenter;

//    titleLable.backgroundColor = [UIColor redColor];

titleLable.font = [UIFont fontWithName:kFontNameStrBold size:22];

titleLable.textColor = [UIColor whiteColor];

self.titleLable = titleLable;

[self.blackeView addSubview:titleLable];

/**内容的文本*/

UILabel * descLable = [[UILabel alloc]init];

self.desLabel = descLable;

descLable.numberOfLines = 0;

descLable.textAlignment = NSTextAlignmentCenter;

//    descLable.backgroundColor = [UIColor orangeColor];

descLable.font = [UIFont fontWithName:kFontNameStrRegular size:15];

descLable.textColor = [UIColor whiteColor];

[self.blackeView addSubview:descLable];

/**确认按钮的添加*/

UIButton * yesButton = [[UIButton alloc]init];

yesButton.tag = 2;

[yesButton addTarget:self action:@selector(yesOrNOButtonClcik:) forControlEvents:UIControlEventTouchUpInside];

self.yesButton  = yesButton;

[yesButton setBackgroundColor:[UIColor colorWithRed:25/255.0 green:164/255.0 blue:58/255.0 alpha:1]];

yesButton.titleLabel.font = [UIFont fontWithName:kFontNameStrBold size:17];

[yesButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

yesButton.layer.cornerRadius = kButtonRadius;

yesButton.layer.masksToBounds = YES;

[self.blackeView addSubview:yesButton];

/**取消按钮的添加*/

UIButton * noButton =[[UIButton alloc]init];

noButton.tag = 1;

[noButton addTarget:self action:@selector(yesOrNOButtonClcik:) forControlEvents:UIControlEventTouchUpInside];

[noButton setBackgroundColor:[UIColor colorWithRed:38/255.0 green:38/255.0 blue:38/255.0 alpha:1]];

NSString * buttonstr = [NSString stringWithFormat:@"%@",noButton.titleLabel.text];

NSInteger buttonLength =buttonstr.length;

if (5< buttonLength) {

noButton.titleLabel.font = [UIFont fontWithName:kFontNameStrBold size:15];

yesButton.titleLabel.font= [UIFont fontWithName:kFontNameStrBold size:15];

}else{

noButton.titleLabel.font = [UIFont fontWithName:kFontNameStrBold size:17];

}

[noButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

noButton.layer.cornerRadius = kButtonRadius;

noButton.layer.masksToBounds = YES;

self.noButton = noButton;

[self.blackeView addSubview:noButton];

}

-(void)layoutSubviews

{

//设置黑色View的frame

self.blackeView.frame =CGRectMake(0, 0, self.bounds.size.width/1.2, 170);

self.blackeView.center = CGPointMake(self.center.x, self.center.y);

//设置titleLable的frame

NSString * titlestr = [NSString stringWithFormat:@"%@",self.titleLable.text];

NSInteger titlerlength =titlestr.length;

if (30<titlerlength) {

self.titleLable.frame = CGRectMake(10,0,self.blackeView.bounds.size.width/1.07 ,60);

}else{

self.titleLable.frame = CGRectMake(10, 10,self.blackeView.bounds.size.width/1.07 ,40);

}

//设置内容的lable

self.desLabel.frame = CGRectMake(10,CGRectGetMaxY(self.titleLable.bounds),self.blackeView.bounds.size.width/1.07, 50);

//设置NO的按钮frame

CGFloat nobtnH = kNoButtonH;

CGFloat nobtnW = kNoButtonW;

self.noButton.frame = CGRectMake(10, CGRectGetMaxY(self.blackeView.bounds)-nobtnH-15, nobtnW, nobtnH);

//设置YES的按钮frame

self.yesButton.frame = CGRectMake(CGRectGetMaxX(self.blackeView.bounds)-nobtnW -10, CGRectGetMaxY(self.blackeView.bounds)-nobtnH-15 ,self.noButton.bounds.size.width, self.noButton.bounds.size.height);

/**根据判断来设置frame*/

[self stupOrEmpty];

}

-(void)stupOrEmpty

{

if ([self.noButton.titleLabel.text isEqualToString:@""] ||self.noButton.titleLabel.text ==nil)

{//如果No按钮没有文字则只显示YES按钮

self.noButton.hidden = YES;

self.yesButton.frame = CGRectMake(0,0, kNoButtonW +30, kNoButtonH);

self.yesButton.center = CGPointMake(self.blackeView.bounds.size.width/2, CGRectGetMaxY(self.blackeView.bounds)-kNoButtonH);

}else if ([self.yesButton.titleLabel.text isEqualToString:@""]|| self.yesButton.titleLabel.text == nil)

{//如果YES按钮没有文字则只显示NO按钮

self.yesButton.hidden = YES;

self.noButton.frame = CGRectMake(0, 0, kNoButtonW+30, kNoButtonH);

self.noButton.center = CGPointMake(self.blackeView.bounds.size.width/2, CGRectGetMaxY(self.blackeView.bounds)-kNoButtonH);

[self.noButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

[self.noButton setBackgroundColor:[UIColor colorWithRed:25/255.0 green:164/255.0 blue:58/255.0 alpha:1]];

}

//判断文本是否有内容

if([self.titleLable.text isEqualToString:@""]||self.titleLable.text ==nil)

{

self.desLabel.font = [UIFont fontWithName:kFontNameStrBold size:20];

self.desLabel.frame = CGRectMake(10,CGRectGetMaxY(self.titleLable.bounds)-20,self.blackeView.bounds.size.width/1.07, 90);

self.titleLable.hidden = YES;

}

}

-(void)yesOrNOButtonClcik:(UIButton*)buttonTag

{

if ([self.delegate respondsToSelector:@selector(popupView:ClickedButtonAtIndex:)]) {

[self.delegate popupView:self ClickedButtonAtIndex:buttonTag.tag];

}

[self removeSelf];

}

- (void)removeSelf

{

[UIView animateWithDuration:0.38 animations:^{

self.alpha = 0;

}completion:^(BOOL finished) {

[self removeFromSuperview];

}];

}

- (void)show

{

UIWindow *window = [UIApplication sharedApplication].keyWindow;

[window addSubview:self];

self.alpha = 0;

self.blackeView.transform = CGAffineTransformMakeScale(1.08, 1.08);

[UIView animateWithDuration:0.38 animations:^{

self.alpha = 1;

self.blackeView.transform = CGAffineTransformIdentity;

}];

}

@end

/**上面这个就是放在.m里面了*/

<PopupViewDelegate>

接下来你只需要在你要响应的点击事件里面去导入这个View的头文件和遵守它的代理实现代理方法就可以了

PopupView * popupV = [[PopupView alloc]initWithTitle:@"Are You?" message:@"你猜我我猜你猜我猜你猜我是谁!" cancelButtonTitle:@"Helvetica" okButtonTitles:@"OK" delegate:self];

[popupV show];

/**代理方法*/

- (void)popupView:(PopupView *)popupView ClickedButtonAtIndex:(NSInteger)buttonIndex{

if (popupView.tag == 1) {

NSLog(@"点击了取消按钮");

}else{

NSLog(@"点击了确认按钮");

}

}

好了一个自定义的弹出框就搞定了!

自定义一个类似UIAlertView的弹出框的更多相关文章

  1. vh属性-- 一个永远垂直居中的弹出框

    下面的demo,无论浏览器大小如何改变,滚动条是否滚动,弹出框永远水平垂直居中 <html> <head> <title></title> </h ...

  2. sweetalert : 一个比较好看的弹出框

    1.引入 <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"> </script& ...

  3. [Js插件]使用JqueryUI的弹出框做一个“炫”的登录页面

    引言 查看项目代码的时候,发现项目中用到JqueryUi的弹出框,可拖拽,可设置模式对话框,就想着使用它弄一个登录页面. 弹出框 在Jquery Ui官网可定制下载弹出框,下载和弹出框下载相关的js文 ...

  4. WINRAR评估版本弹出框消除

    网上有很多WINRAR评估版本,这些版本下载安装了之后总会有些广告弹出,让人很烦恼,现在教大家一个方法消除这些弹出框. 复制以下代码:    RAR registration data SeVeN U ...

  5. 原生Js封装的弹出框-弹出窗口-页面居中-多状态可选

    原生Js封装的弹出框-弹出窗口-页面居中-多状态可选   实现了一下功能: 1.title可自定义 可拖拽 2.width height可以自定义 3.背景遮罩和透明度可以自定义 4.可以自己编辑弹出 ...

  6. vue--vant组件库Dialog弹出框

    安装vant UI框架: cnpm install vant –-save-dev 导入组件-在main.js里: import Vant from 'vant'; import'vant/lib/v ...

  7. WindowsPhone模拟简易Toast弹出框

    Coding4Fun这个开源控件中有ToastPrompt这个弹出框组件,但是由于Coding4Fun太庞大,如果只用到ToastPrompt这个控件的话,整个引用不太值当的.于是自己写了一个差不多的 ...

  8. ASP.NET中的几种弹出框提示

    B/S不像C/S那样一个MessageBox就可以弹出提示框,不过可以通过js的“Alert”来弹出消息,或者通过一些变种的js方法.下面我给大家介绍几种,希望大家喜欢. 四种弹出框代码: prote ...

  9. js实现弹出框跟随鼠标移动

    又是新的一天网上冲浪,在bing的搜索页面下看到这样一个效果: 即弹出框随着鼠标的移动而移动.思路大概为: 调用onmousemove函数,将鼠标的当前位置赋予弹出框即可 //html <div ...

随机推荐

  1. 基于BootStrap框架构建快速响应的GPS部标监控平台

    最近一个客户要求将gps部标平台移植到bootStrap框架作为前端框架,符合交通部796部标只是他们的一个基本要求,重点是要和他们的冷链云物流平台进行适配.我自己先浏览了客户的云物流平台的界面,采用 ...

  2. 改造过的JS颜色选择器

    项目中用到颜色选择功能,在网上找了个颜色选择器,自己改了改代码.做成了一个可动态加载的颜色选择器. 把代码贴上,当是记录. /*Copyright(c)2009,www.supersite.me*/ ...

  3. HDU - 1875 畅通工程再续

    Problem Description 相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问 ...

  4. Struts2下载文件点取消出现的异常解决

    struts2点击下载,如果正常下载不会报错,可是如果点击取消就会报如下的错误: Java.lang.IllegalStateException: Cannot call sendError() af ...

  5. 3.多线程NSOperation

    1.NSOperation的基本操作 使用NSOperation的两个子类,NSInvocationOperation 和 NSBlockOperation 创建操作,然后将操作添加到队列中去执行 / ...

  6. ADB server didn't ACK

    当我们通过eclipse开发Android应用时,会连接真机会使用模拟器进行仿真,有时候启动失败,会提示这样的错误. 工具/原料 Eclipse CMD命令窗口 方法/步骤 首先通过CMD启动adb服 ...

  7. T-SQL存储过程、游标

    Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...

  8. 总结/PSP初体验—排球计分程序1.0

    要做一个排球计分程序,墨迹了很长时间才做出个的东西,过程很不爽: 功能:这个软件有两个页面,可以实现窗体A的部分变化控制窗体B的部分变化.A是操作人员使用看到的,B是投放给观众的,完全由A操控: 学到 ...

  9. asp.net MVC 过滤器使用案例:统一处理异常顺道精简代码

    重构的乐趣在于精简代码,模块化设计,解耦功能……而对异常处理的重构则刚好满足上述三个方面,下面是我的一点小心得. 一.相关的学习 在文章<精简自己20%的代码>中,讨论了异常的统一处理,并 ...

  10. C语言 结构体指针赋值 incompatible types when assigning to type 'char[20]' from type 'char *'

    strcpy(pstudent->name, "guo zhao wei "); 为什么错误,该怎么写,(红色行)     追问 为什么不能直接赋值啊, 追答 用char n ...