iOS自定義alertView提示框?qū)嵗窒?/h1>
更新時(shí)間:2016年04月21日 15:32:12 作者:菜鳥(niǎo)Alex
這是一款可以隨意改變彈框背景色,按鈕背景色,提示消息字體顏色的iOS alertView提示框,想要用于這樣一款alertView提示框的朋友不要錯(cuò)過(guò)
本文實(shí)例為大家分享iOS自定義alertView提示框,先上圖,彈框的背景色,按鈕背景色,提示的消息的字體顏色都可以改變

利用單例實(shí)現(xiàn)豐富的自定義接口
//
// PBAlertController.h
// PBAlertDemo
//
// Created by 裴波波 on 16/4/20.
// Copyright © 2016年 裴波波. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef void(^PBBlock)();
@interface PBAlertController : UIViewController
/** 設(shè)置alertView背景色 */
@property (nonatomic, copy) UIColor *alertBackgroundColor;
/** 設(shè)置確定按鈕背景色 */
@property (nonatomic, copy) UIColor *btnConfirmBackgroundColor;
/** 設(shè)置取消按鈕背景色 */
@property (nonatomic, copy) UIColor *btnCancelBackgroundColor;
/** 設(shè)置message字體顏色 */
@property (nonatomic, copy) UIColor *messageColor;
/** 創(chuàng)建單例 */
+(instancetype)shareAlertController;
/** 彈出alertView以及點(diǎn)擊確定回調(diào)的block */
-(void)alertViewControllerWithMessage:(NSString *)message andBlock:(PBBlock) block;
@end
.m文件中初始化控件以及對(duì)alertView的控件的屬性進(jìn)行懶加載,確定初始的顏色.
//
// 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;
/** 點(diǎn)擊確定回調(diào)的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;
//創(chuàng)建蒙版
UIView * coverView = [[UIView alloc] initWithFrame:kMainScreenBounds];
self.coverView = coverView;
[self.view addSubview:coverView];
coverView.backgroundColor = [UIColor blackColor];
coverView.alpha = 0.7;
//創(chuàng)建提示框view
UIView * alertView = [[UIView alloc] init];
alertView.backgroundColor = self.alertBackgroundColor;
//設(shè)置圓角半徑
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);
//創(chuàng)建操作提示 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);
//創(chuàng)建中間灰色分割線
UIView * separateLine = [[UIView alloc] init];
separateLine.backgroundColor = [UIColor grayColor];
[alertView addSubview:separateLine];
separateLine.frame = CGRectMake(0, lblHigth + 1, alertView.bounds.size.width, 1);
//創(chuàng)建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);
//創(chuàng)建確定 取消按鈕
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];
}
/** 點(diǎn)擊確定 or 取消觸發(fā)事件 */
-(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
在需要調(diào)用的地方進(jìn)行調(diào)用
//
// 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
//點(diǎn)擊按鈕彈出提示框
- (IBAction)clickShowAlertBtn:(id)sender {
PBAlertController * alertVc = [PBAlertController shareAlertController];
alertVc.messageColor = [UIColor redColor];
[alertVc alertViewControllerWithMessage:@"這是一message沙哈" andBlock:^{
NSLog(@"點(diǎn)擊確定后執(zhí)行的方法");
}];
alertVc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:alertVc animated:YES];
}
@end
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)iOS程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- iOS9提示框的正確使用方式
- iOS移動(dòng)端(H5)alert/confirm提示信息去除網(wǎng)址(URL)
- iOS自定義提示彈出框?qū)崿F(xiàn)類(lèi)似UIAlertView的效果
- iOS自定義推送消息提示框
- Android仿IOS自定義AlertDialog提示框
- iOS實(shí)現(xiàn)圓角箭頭矩形的提示框
- iOS實(shí)現(xiàn)UITableView數(shù)據(jù)為空時(shí)的提示頁(yè)面
- iOS微信分享后關(guān)閉發(fā)送成功提示并返回應(yīng)用
- IOS開(kāi)發(fā)實(shí)現(xiàn)手機(jī)震動(dòng)的提示實(shí)例代碼
- iOS自定義圓形進(jìn)度提示控件
相關(guān)文章
-
iOS中使用NSURLConnection處理HTTP同步與異步請(qǐng)求
NSURLConnection的作用現(xiàn)在已經(jīng)基本被NSURLSession所取代,所以我們簡(jiǎn)單了解下iOS中使用NSURLConnection處理HTTP同步與異步請(qǐng)求的方法即可: 2016-07-07
-
iOS實(shí)現(xiàn)實(shí)時(shí)檢測(cè)網(wǎng)絡(luò)狀態(tài)的示例代碼
網(wǎng)絡(luò)連接狀態(tài)檢測(cè)對(duì)于我們的iOS開(kāi)發(fā)來(lái)說(shuō)是一個(gè)非常通用的需求。下面這篇文章主要就給大家介紹了關(guān)于利用iOS實(shí)現(xiàn)實(shí)時(shí)檢測(cè)網(wǎng)絡(luò)狀態(tài)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。 2017-07-07
-
iOS實(shí)現(xiàn)卡片式滾動(dòng)效果 iOS實(shí)現(xiàn)電影選片效果
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)卡片式滾動(dòng)效果,實(shí)現(xiàn)電影選片效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下 2020-02-02
-
iOS 導(dǎo)航欄無(wú)縫圓滑的隱藏 Navigationbar實(shí)例代碼
本文通過(guò)實(shí)例代碼給大家介紹了iOS 導(dǎo)航欄無(wú)縫圓滑的隱藏 Navigationbar的效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下 2017-11-11
-
iOS 中使用tableView實(shí)現(xiàn)右滑顯示選擇功能
這篇文章主要介紹了iOS 中使用tableView實(shí)現(xiàn)右滑顯示選擇功能的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下 2016-07-07
-
配置iOS?16?屏幕旋轉(zhuǎn)適配實(shí)例詳解
這篇文章主要為大家介紹了配置iOS?16?屏幕旋轉(zhuǎn)適配實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪 2022-09-09
-
iOS開(kāi)發(fā)使用GDataXML框架解析網(wǎng)絡(luò)數(shù)據(jù)
GDataXML是Google開(kāi)發(fā)的一個(gè)XML解析庫(kù),輕便,特點(diǎn)使用非常簡(jiǎn)單,支持XPath。今天把前兩天弄的IOS XML解析記錄下來(lái),也供大家參考。 2016-02-02
最新評(píng)論
本文實(shí)例為大家分享iOS自定義alertView提示框,先上圖,彈框的背景色,按鈕背景色,提示的消息的字體顏色都可以改變

利用單例實(shí)現(xiàn)豐富的自定義接口
// // PBAlertController.h // PBAlertDemo // // Created by 裴波波 on 16/4/20. // Copyright © 2016年 裴波波. All rights reserved. // #import <UIKit/UIKit.h> typedef void(^PBBlock)(); @interface PBAlertController : UIViewController /** 設(shè)置alertView背景色 */ @property (nonatomic, copy) UIColor *alertBackgroundColor; /** 設(shè)置確定按鈕背景色 */ @property (nonatomic, copy) UIColor *btnConfirmBackgroundColor; /** 設(shè)置取消按鈕背景色 */ @property (nonatomic, copy) UIColor *btnCancelBackgroundColor; /** 設(shè)置message字體顏色 */ @property (nonatomic, copy) UIColor *messageColor; /** 創(chuàng)建單例 */ +(instancetype)shareAlertController; /** 彈出alertView以及點(diǎn)擊確定回調(diào)的block */ -(void)alertViewControllerWithMessage:(NSString *)message andBlock:(PBBlock) block; @end
.m文件中初始化控件以及對(duì)alertView的控件的屬性進(jìn)行懶加載,確定初始的顏色.
//
// 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;
/** 點(diǎn)擊確定回調(diào)的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;
//創(chuàng)建蒙版
UIView * coverView = [[UIView alloc] initWithFrame:kMainScreenBounds];
self.coverView = coverView;
[self.view addSubview:coverView];
coverView.backgroundColor = [UIColor blackColor];
coverView.alpha = 0.7;
//創(chuàng)建提示框view
UIView * alertView = [[UIView alloc] init];
alertView.backgroundColor = self.alertBackgroundColor;
//設(shè)置圓角半徑
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);
//創(chuàng)建操作提示 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);
//創(chuàng)建中間灰色分割線
UIView * separateLine = [[UIView alloc] init];
separateLine.backgroundColor = [UIColor grayColor];
[alertView addSubview:separateLine];
separateLine.frame = CGRectMake(0, lblHigth + 1, alertView.bounds.size.width, 1);
//創(chuàng)建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);
//創(chuàng)建確定 取消按鈕
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];
}
/** 點(diǎn)擊確定 or 取消觸發(fā)事件 */
-(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
在需要調(diào)用的地方進(jìn)行調(diào)用
//
// 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
//點(diǎn)擊按鈕彈出提示框
- (IBAction)clickShowAlertBtn:(id)sender {
PBAlertController * alertVc = [PBAlertController shareAlertController];
alertVc.messageColor = [UIColor redColor];
[alertVc alertViewControllerWithMessage:@"這是一message沙哈" andBlock:^{
NSLog(@"點(diǎn)擊確定后執(zhí)行的方法");
}];
alertVc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:alertVc animated:YES];
}
@end
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)iOS程序設(shè)計(jì)有所幫助。
- iOS9提示框的正確使用方式
- iOS移動(dòng)端(H5)alert/confirm提示信息去除網(wǎng)址(URL)
- iOS自定義提示彈出框?qū)崿F(xiàn)類(lèi)似UIAlertView的效果
- iOS自定義推送消息提示框
- Android仿IOS自定義AlertDialog提示框
- iOS實(shí)現(xiàn)圓角箭頭矩形的提示框
- iOS實(shí)現(xiàn)UITableView數(shù)據(jù)為空時(shí)的提示頁(yè)面
- iOS微信分享后關(guān)閉發(fā)送成功提示并返回應(yīng)用
- IOS開(kāi)發(fā)實(shí)現(xiàn)手機(jī)震動(dòng)的提示實(shí)例代碼
- iOS自定義圓形進(jìn)度提示控件
相關(guān)文章
iOS中使用NSURLConnection處理HTTP同步與異步請(qǐng)求
NSURLConnection的作用現(xiàn)在已經(jīng)基本被NSURLSession所取代,所以我們簡(jiǎn)單了解下iOS中使用NSURLConnection處理HTTP同步與異步請(qǐng)求的方法即可:2016-07-07
iOS實(shí)現(xiàn)實(shí)時(shí)檢測(cè)網(wǎng)絡(luò)狀態(tài)的示例代碼
網(wǎng)絡(luò)連接狀態(tài)檢測(cè)對(duì)于我們的iOS開(kāi)發(fā)來(lái)說(shuō)是一個(gè)非常通用的需求。下面這篇文章主要就給大家介紹了關(guān)于利用iOS實(shí)現(xiàn)實(shí)時(shí)檢測(cè)網(wǎng)絡(luò)狀態(tài)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-07-07
iOS實(shí)現(xiàn)卡片式滾動(dòng)效果 iOS實(shí)現(xiàn)電影選片效果
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)卡片式滾動(dòng)效果,實(shí)現(xiàn)電影選片效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
iOS 導(dǎo)航欄無(wú)縫圓滑的隱藏 Navigationbar實(shí)例代碼
本文通過(guò)實(shí)例代碼給大家介紹了iOS 導(dǎo)航欄無(wú)縫圓滑的隱藏 Navigationbar的效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11
iOS 中使用tableView實(shí)現(xiàn)右滑顯示選擇功能
這篇文章主要介紹了iOS 中使用tableView實(shí)現(xiàn)右滑顯示選擇功能的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
配置iOS?16?屏幕旋轉(zhuǎn)適配實(shí)例詳解
這篇文章主要為大家介紹了配置iOS?16?屏幕旋轉(zhuǎn)適配實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
iOS開(kāi)發(fā)使用GDataXML框架解析網(wǎng)絡(luò)數(shù)據(jù)
GDataXML是Google開(kāi)發(fā)的一個(gè)XML解析庫(kù),輕便,特點(diǎn)使用非常簡(jiǎn)單,支持XPath。今天把前兩天弄的IOS XML解析記錄下來(lái),也供大家參考。2016-02-02

