请选择 进入手机版 | 继续访问电脑版
运用UIViewController进行自定义的AlertView功能。
完全自定义,尽可以的发挥AlertView功能的扩展性。而且还可以进地扩展,有更大可发挥空间。
效果图

自定义AlertView

自定义AlertView

代码及步骤如下:
1、实现自定义AlertView
.h文件中
[Objective-C] 纯文本查看 复制代码
//
//  HCYAlertViewController.h
//  HCAlertView
//
//  Created by 36ji.net on 16/12/31.
//  Copyright © 2016年 36ji.net. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef void(^HCYBlock)();
@interface HCYAlertViewController : 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:(HCYBlock) block;

@end

.m文件中
[Objective-C] 纯文本查看 复制代码
#import "HCYAlertViewController.h"

/** 屏幕尺寸 */
#define kMainScreenBounds [UIScreen mainScreen].bounds

@interface HCYAlertViewController ()

/** 蒙版 */
@property (nonatomic, strong) UIView *coverView;
/** 弹框 */
@property (nonatomic, strong) UIView *alertView;
/** message label**/
@property (nonatomic, strong) UILabel * labMessage;
//message
@property (nonatomic, strong) NSString * theMessage;
/** 点击确定回调的block */
@property (nonatomic, copy) HCYBlock block;

@end

@implementation HCYAlertViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.labMessage.text = self.theMessage;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self makeMyView];
}

- (void)makeMyView{
    //创建蒙版
    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];
    self.labMessage = lblMessage;
//    lblMessage.text = self.labMessage.text;
    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];

}

- (void)alertViewControllerWithMessage:(NSString *)message andBlock:(HCYBlock)block{
    self.block = block;
    self.theMessage = message;
}

//点击确定 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];
    // Dispose of any resources that can be recreated.
}

static HCYAlertViewController * instance = nil;
+(instancetype)shareAlertController{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[HCYAlertViewController 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 greenColor];
    }
    return _btnConfirmBackgroundColor;
}

// 取消按钮背景色
-(UIColor *)btnCancelBackgroundColor{
    
    if (_btnCancelBackgroundColor == nil) {
        _btnCancelBackgroundColor = [UIColor blueColor];
    }
    return _btnCancelBackgroundColor;
}

// message字体颜色
-(UIColor *)messageColor{
    
    if (_messageColor == nil) {
        _messageColor = [UIColor blackColor];
    }
    return _messageColor;
}

2、自定义AlertView的使用
[Objective-C] 纯文本查看 复制代码
- (void)buttonClick:(id)sender{
    HCYAlertViewController * hcyAlertVC = [HCYAlertViewController shareAlertController];
    hcyAlertVC.messageColor = [UIColor redColor];
    [hcyAlertVC alertViewControllerWithMessage:@"这里传递一个消息内容" andBlock:^{
        NSLog(@"点击确定后回调执行此处代码");
    }];
    hcyAlertVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:hcyAlertVC animated:YES completion:^{
        NSLog(@"界面调起完成");
    }];
}


举报 使用道具
| 回复

共 0 个关于本帖的回复 最后回复于 2016-12-31 21:51

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

本文内容不够精彩,我要自己发布

发布新帖

推荐阅读

    拥有的,不仅是技术!还有...
    联系 Email: support.36ji@qq.com

    • 关注酷站官方微博
      了解最新动态

    • 关注酷站微信公众号
      这里有好玩的讯息

    • 加入酷站交流群
      不断在这里成长

    © 2014-2017 36ji网络科技有限公司 . All rights reserved.
    京ICP备14001609号

    Archiver|    
    Powered by Discuz! X3.2 © 2001-2013 Comsenz Inc.
    快速回复 返回顶部 返回列表