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

 更新时间:2016年04月21日 15:32:12   作者:菜鸟Alex  
这是一款可以随意改变弹框背景色,按钮背景色,提示消息字体颜色的iOS alertView提示框,想要用于这样一款alertView提示框的朋友不要错过

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


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

//
// PBAlertController.h
// PBAlertDemo
//
// Created by 裴波波 on 16/4/20.
// Copyright © 2016年 裴波波. All rights reserved.
//

#import <UIKit/UIKit.h>


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程序设计有所帮助。

相关文章

  • iOS11适配工作及导航栏影藏返回文字的解决方法

    iOS11适配工作及导航栏影藏返回文字的解决方法

    这篇文章主要介绍了iOS11适配工作及导航栏影藏返回文字的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-10-10
  • iOS中使用NSURLConnection处理HTTP同步与异步请求

    iOS中使用NSURLConnection处理HTTP同步与异步请求

    NSURLConnection的作用现在已经基本被NSURLSession所取代,所以我们简单了解下iOS中使用NSURLConnection处理HTTP同步与异步请求的方法即可:
    2016-07-07
  • 谈谈iOS中的几种锁

    谈谈iOS中的几种锁

    这篇文章主要介绍了谈谈iOS中的几种锁,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2016-11-11
  • iOS实现手势密码功能

    iOS实现手势密码功能

    这篇文章主要为大家详细介绍了iOS实现手势密码功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • iOS实现实时检测网络状态的示例代码

    iOS实现实时检测网络状态的示例代码

    网络连接状态检测对于我们的iOS开发来说是一个非常通用的需求。下面这篇文章主要就给大家介绍了关于利用iOS实现实时检测网络状态的相关资料,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-07-07
  • iOS实现卡片式滚动效果 iOS实现电影选片效果

    iOS实现卡片式滚动效果 iOS实现电影选片效果

    这篇文章主要为大家详细介绍了iOS实现卡片式滚动效果,实现电影选片效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • iOS 导航栏无缝圆滑的隐藏 Navigationbar实例代码

    iOS 导航栏无缝圆滑的隐藏 Navigationbar实例代码

    本文通过实例代码给大家介绍了iOS 导航栏无缝圆滑的隐藏 Navigationbar的效果,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-11-11
  • iOS 中使用tableView实现右滑显示选择功能

    iOS 中使用tableView实现右滑显示选择功能

    这篇文章主要介绍了iOS 中使用tableView实现右滑显示选择功能的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-07-07
  • 配置iOS 16 屏幕旋转适配实例详解

    配置iOS 16 屏幕旋转适配实例详解

    这篇文章主要为大家介绍了配置iOS 16 屏幕旋转适配实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • iOS开发使用GDataXML框架解析网络数据

    iOS开发使用GDataXML框架解析网络数据

    GDataXML是Google开发的一个XML解析库,轻便,特点使用非常简单,支持XPath。今天把前两天弄的IOS XML解析记录下来,也供大家参考。
    2016-02-02

最新评论