iOS自定义alertView提示框实例分享

本文实例为大家分享iOS自定义alertView提示框,先上图,弹框的背景色,按钮背景色,提示的消息的字体颜色都可以改变

利用单例实现丰富的自定义接口

//

// PBAlertController.h

// PBAlertDemo

//

// Created by 裴波波 on 16/4/20.

// Copyright © 2016年 裴波波. All rights reserved.

//

#import

typedef void(^PBBlock)();

@interface PBAlertController : UIViewController

/** 设置alertView背景色 */

@property (nonatomic, copy) UIColor *alertBackgroundColor;

/** 设置确定按钮背景色 */

@property (nonatomic, copy) UIColor *btnConfirmBackgroundColor;

/** 设置取消按钮背景色 */

@property (nonatomic, copy) UIColor *btnCancelBackgroundColor;

/** 设置message字体颜色 */

@property (nonatomic, copy) UIColor *messageColor;

/** 创建单例 */

+(instancetype)shareAlertController;

/** 弹出alertView以及点击确定回调的block */

-(void)alertViewControllerWithMessage:(NSString *)message andBlock:(PBBlock) block;

@end

.m文件中初始化控件以及对alertView的控件的属性进行懒加载,确定初始的颜色.

//

// PBAlertController.m

// PBAlertDemo

//

// Created by 裴波波 on 16/4/20.

// Copyright © 2016年 裴波波. All rights reserved.

//

#import "PBAlertController.h"

/** 屏幕尺寸 */

#define kMainScreenBounds [UIScreen mainScreen].bounds

@interface PBAlertController ()

/** 蒙版 */

@property (nonatomic, strong) UIView *coverView;

/** 弹框 */

@property (nonatomic, strong) UIView *alertView;

/** 点击确定回调的block */

@property (nonatomic, copy) PBBlock block;

@end

@implementation PBAlertController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];

}

-(void)alertViewControllerWithMessage:(NSString *)message andBlock:(PBBlock) block{

self.block = block;

//创建蒙版

UIView * coverView = [[UIView alloc] initWithFrame:kMainScreenBounds];

self.coverView = coverView;

[self.view addSubview:coverView];

coverView.backgroundColor = [UIColor blackColor];

coverView.alpha = 0.7;

//创建提示框view

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

alertView.backgroundColor = self.alertBackgroundColor;

//设置圆角半径

alertView.layer.cornerRadius = 6.0;

self.alertView = alertView;

[self.view addSubview:alertView];

alertView.center = coverView.center;

alertView.bounds = CGRectMake(0, 0, kMainScreenBounds.size.width * 0.75, kMainScreenBounds.size.width * 0.75 * 1.5/ 3);

//创建操作提示 label

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

[alertView addSubview:label];

label.text = @"操作提示";

label.font = [UIFont systemFontOfSize:19];

label.textAlignment = NSTextAlignmentCenter;

CGFloat lblWidth = alertView.bounds.size.width;

CGFloat lblHigth = 22;

label.frame = CGRectMake(0, 0, lblWidth, lblHigth);

//创建中间灰色分割线

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

separateLine.backgroundColor = [UIColor grayColor];

[alertView addSubview:separateLine];

separateLine.frame = CGRectMake(0, lblHigth + 1, alertView.bounds.size.width, 1);

//创建message label

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

lblMessage.textColor = self.messageColor;

[alertView addSubview:lblMessage];

lblMessage.text = message;

lblMessage.textAlignment = NSTextAlignmentCenter;

lblMessage.numberOfLines = 2; //最多显示两行Message

CGFloat margin = 5;

CGFloat msgX = margin;

CGFloat msgY = lblHigth + 3;

CGFloat msgW = alertView.bounds.size.width - 2 * margin;

CGFloat msgH = 44;

lblMessage.frame = CGRectMake(msgX, msgY, msgW, msgH);

//创建确定 取消按钮

CGFloat buttonWidth = (alertView.bounds.size.width - 4 * margin) * 0.5;

CGFloat buttonHigth = 25;

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

[alertView addSubview:btnCancel];

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

[btnCancel setTitle:@"取消" forState:UIControlStateNormal];

[btnCancel setBackgroundColor:self.btnCancelBackgroundColor];

btnCancel.frame = CGRectMake(margin, alertView.bounds.size.height - margin - buttonHigth, buttonWidth, buttonHigth);

btnCancel.tag = 0;

[btnCancel addTarget:self action:@selector(didClickBtnConfirm:) forControlEvents:UIControlEventTouchUpInside];

//确定按钮

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

btnConfirm.tag = 1;

[alertView addSubview:btnConfirm];

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

[btnConfirm setTitle:@"确定" forState:UIControlStateNormal];

[btnConfirm setBackgroundColor:self.btnConfirmBackgroundColor];

btnConfirm.frame = CGRectMake(alertView.bounds.size.width - margin - buttonWidth, alertView.bounds.size.height - margin - buttonHigth, buttonWidth, buttonHigth);

[btnConfirm addTarget:self action:@selector(didClickBtnConfirm:) forControlEvents:UIControlEventTouchUpInside];

}

/** 点击确定 or 取消触发事件 */

-(void)didClickBtnConfirm:(UIButton *)sender{

if (sender.tag == 0) {

[self dismissViewControllerAnimated:YES completion:nil];

return;

}

self.block();

[self dismissViewControllerAnimated:YES completion:nil];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

}

static PBAlertController * instance = nil;

+(instancetype)shareAlertController{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

instance = [[PBAlertController alloc] init];

});

return instance;

}

-(UIColor *)alertBackgroundColor{

if (_alertBackgroundColor == nil) {

_alertBackgroundColor = [UIColor colorWithRed:249/255.0 green:249/255.0 blue:249/255.0 alpha:1];

}

return _alertBackgroundColor;

}

/** 确定按钮背景色 */

-(UIColor *)btnConfirmBackgroundColor{

if (_btnConfirmBackgroundColor == nil) {

_btnConfirmBackgroundColor = [UIColor orangeColor];

}

return _btnConfirmBackgroundColor;

}

/** 取消按钮背景色 */

-(UIColor *)btnCancelBackgroundColor{

if (_btnCancelBackgroundColor == nil) {

_btnCancelBackgroundColor = [UIColor blueColor];

}

return _btnCancelBackgroundColor;

}

/** message字体颜色 */

-(UIColor *)messageColor{

if (_messageColor == nil) {

_messageColor = [UIColor blackColor];

}

return _messageColor;

}

@end

在需要调用的地方进行调用

//

// ViewController.m

// PBAlertDemo

//

// Created by 裴波波 on 16/4/20.

// Copyright © 2016年 裴波波. All rights reserved.

//

#import "ViewController.h"

#import "PBAlertController.h"

@interface ViewController ()

@end

@implementation ViewController

//点击按钮弹出提示框

- (IBAction)clickShowAlertBtn:(id)sender {

PBAlertController * alertVc = [PBAlertController shareAlertController];

alertVc.messageColor = [UIColor redColor];

[alertVc alertViewControllerWithMessage:@"这是一message沙哈" andBlock:^{

NSLog(@"点击确定后执行的方法");

}];

alertVc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

[self presentModalViewController:alertVc animated:YES];

}

@end

以上就是本文的全部内容,希望对大家学习iOS程序设计有所帮助。相关阅读:

linux系统中InputStream输入流的方法之reset()和mark()命令的注意事项

Java函数式编程(七):MapReduce

JavaScript实现DIV层拖动及动态增加新层的方法

Linux系统下ssh登陆很慢的解决办法

jquery复选框多选赋值给文本框的方法

AngularJS教程之简单应用程序示例

javascript基本包装类型介绍

win10预览版10041无斯巴达浏览器 IE浏览器Edge渲染引擎更新

在Linux和Unix中使用chmod命令改变文件权限的用法

微软官方晒出Windows 10 for IoT一张高清大图和更多细节

PHP 利用Mail_MimeDecode类提取邮件信息示例

jsp简单实现页面之间共享信息的方法

正则表达式 运算符优先级介绍

C语言将数组中元素的数排序输出的相关问题解决

php使用自定义alert,IOS_iOS自定义alertView提示框实例分享,本文实例为大家分享iOS自定义a - phpStudy...相关推荐

  1. QT 自定义加载等待(Loading)提示框

    QT自定义加载等待提示框 一.效果展示 二.源代码 #ifndef LOADINGDIALOG_H #define LOADINGDIALOG_H #include <QMovie> #i ...

  2. JQuery框架中使用blockUI制作自定义的漂亮的网页提示框

    一.介绍blockUI 它是Jquery框架的一个插件,专门用来做提示框.模态窗口的 地址:http://www.malsup.com/jquery/block/ 具体的使用方法和demo里面都写得很 ...

  3. Qt自定义一个简单的ToolTip提示框

    实现过程 因为 QToolTip 自定义样式不大方便,而且半透明也没法设置,所以需要自定义.而且,Qt 中的顶层 widget 好像默认是不支持透明样式的,可以设置: setWindowFlags(Q ...

  4. 前端提示框定位在鼠标的右下_前端基础高频面试题(更新中)

    页面渲染的全过程 输入url后,先拿到html文件,html下载完以后会开始对它进行解析 html在解析的过程中,如果文本里有外部资源链接,比如css.js和img时,会立即启用其他线程下载这些静态资 ...

  5. qml 自定义消息框_Qt qml 自定义消息提示框

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/a844651990/article/d ...

  6. java中的消息提示框

    2018/04/07 20:18:00 提到消息提示框必须提到的一个类是:javax.swing.JOptionPane 使用示例: JOptionPane.showMessageDialog(nul ...

  7. HightChart数据提示框(Tooltip)

    数据提示框指的当鼠标悬停在某点上时,以框的形式提示该点的数据,比如该点的值,数据单位等.数据提示框内提示的信息完全可以通过格式化函数动态指定:通过设置 tooltip.enabled = false  ...

  8. 弹出提示框 自动消失

    我们在进行增.删.改.查的时候,很多时候都需要一个提示信息以表明所做操作的成功等状况.有些人喜欢用alert()来提示,这不太人性化,因为alert()弹出来的提示框必须点确定才能继续进行其它操作.我 ...

  9. JavaScript实现弹出浏览器的三种提示框:提示信息框、确认框和输入文本框

    浏览器的三种提示框 1.alert()提示信息框 2.confirm()提示确认框 3.prompt()提示输入文本框 1.alert()提示信息框 效果图: 实现代码: <script> ...

最新文章

  1. 剑指offer :从尾到头打印链表
  2. 树莓派安装python模块_树莓派引脚编号、pypi说明和安装
  3. 动态代理——》AOP —— Spring 中的 AOP||AOP 相关术语||学习 spring 中的 AOP 要明确的事
  4. 昨天登陆页面,无法进入后台,今天攻克了
  5. 《JavaScript 高级程序设计》 7.5 常用模式
  6. Nginx location执行顺序和匹配规则
  7. Dell Caps Lock 切换大小写被窃取焦点问题解决办法
  8. java中的垃圾回收机
  9. php 时间类型int类型,mysql 查询 int类型日期转换成datetime类型
  10. oracle创建java程序并执行
  11. 永磁无刷电机及其驱动技术_「技术」某种车型后驱动桥装配工艺及其工装的设计...
  12. 金税盘显示frm000013_开票软件提示FRM-000013怎么回事?
  13. linux 下 pip 安装教程
  14. LinkLib例子之十五:本地文件回放,支持暂停,seek等
  15. 2021 MWC上海 | 5G消息引关注,菊风共谋5G建设发展新篇章
  16. java 获取当前年份 月份,当月第一天和最后一天
  17. easypoi的excel导出单元格只能选下拉选项工具类
  18. Prompt是什么意思?
  19. 计算机科学与技术 难度,计算机科学与技术在职研究生难度如何
  20. mysql默认编码改为gkb编码_MYSQL数据库默认latin1字符集转换为GBK或UTF8

热门文章

  1. Mac下升级python2.7到python3.6,删除2.7,或者不删除2.7都行
  2. indesign照片放入太大_照片打印机,小米、华为到底哪家强?
  3. 服务器定期监控数据_机房环境监控的实践【斯必得智慧机房 】
  4. 张一鸣退出上海字跳公司法定代表人
  5. 小米12能效有望显著提升:骁龙8 Gen1功不可没
  6. FF:与吉利控股的合作取得实质性进展 双方技术团队正紧密对接
  7. 恒大和小米双双否认外界谣言,此前有人宣称小米将接手恒大造车
  8. 发布9个月直降2300!这款手机太惨了:卖完下架
  9. 外媒:日本和芬兰将合作开发6G技术 诺基亚将参与其中
  10. 没有5G也很香!iPhone去年四季度出货量了解下