欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-28 23:10:40
本文实例为大家分享ios自定义alertview提示框,先上图,弹框的背景色,按钮背景色,提示的消息的字体颜色都可以改变 利用单例实现丰富的自定义接口...

本文实例为大家分享ios自定义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程序设计有所帮助。