UIAlertView 在iOS9.0正式被废弃了, 而UIAlertController只有在iOS8.0才能支持, 做项目时不想就这样简单的一个提示还老是再对系统做判断, 所以就自己仿造UIAlertView自定义了一个ZHZAlertView类
并使用block来进行回调
先看效果
- 接口部分
typedef void(^ConfirmBlock)();
typedef void(^CancelBlock)();
@interface ZHZAlertView : UIView
/**
* 自定义初始化 alertView提示框
*
* @param title 标题
* @param message 信息
* @param confirmStr 确认按钮
* @param cancelStr 取消按钮
* @param confirm 点击确认后操作
* @param cancel 点击取消按钮后操作
*
*/
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message confirmAction:(NSString *)confirmStr cancelAction:(NSString *)cancelStr confirmBlock:(ConfirmBlock)confirm cancelBlock:(ConfirmBlock)cancel;
/**
* 便利构造器
*/
+ (instancetype)alertViewWithTitle:(NSString *)title message:(NSString *)message confirmAction:(NSString *)confirmStr cancelAction:(NSString *)cancelStr confirmBlock:(ConfirmBlock)confirm cancelBlock:(ConfirmBlock)cancel;
/**
* 加载并显示视图
*/
- (void)show;
@end
2 . 实现部分
#import "ZHZAlertView.h"
#define kMargin ([UIScreen mainScreen].bounds.size.width / 9.0)
#define kAlertViewPadding 10
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
@interface ZHZAlertView ()
@property (nonatomic, strong)ConfirmBlock confirmBlock;
@property (nonatomic, strong)CancelBlock cancelBlock;
///蒙版
@property (nonatomic, strong)UIView *backView;
///自定义显示的alertView
@property (nonatomic, strong)UIView *customAlertView;
///标题label
@property (nonatomic, strong)UILabel *titleLabel;
///信息label
@property (nonatomic, strong)UILabel *messageLabel;
///点击的按钮segement
@property (nonatomic, strong)UISegmentedControl *segemtedControl;
///需要提示的当前view, 作为ZHZView的对象的父视图
@property (nonatomic, strong)UIView *supView;
///标题string
@property (nonatomic, strong)NSString *title;
///信息string
@property (nonatomic, strong)NSString *message;
///确认按钮string
@property (nonatomic, strong)NSString *confirmStr;
///取消按钮string
@property (nonatomic, strong)NSString *cancelStr;
@end
@implementation ZHZAlertView
//自定义初始化
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message confirmAction:(NSString *)confirmStr cancelAction:(NSString *)cancelStr confirmBlock:(ConfirmBlock)confirm cancelBlock:(ConfirmBlock)cancel {
if (self = [super init]) {
_title = title;
_message = message;
_confirmStr = confirmStr;
_cancelStr = cancelStr;
_confirmBlock = confirm;
_cancelBlock = cancel;
_supView = [UIApplication sharedApplication].keyWindow;
}
return self;
}
//构造便利构造器
+ (instancetype)alertViewWithTitle:(NSString *)title message:(NSString *)message confirmAction:(NSString *)confirmStr cancelAction:(NSString *)cancelStr confirmBlock:(ConfirmBlock)confirm cancelBlock:(ConfirmBlock)cancel {
return [[ZHZAlertView alloc] initWithTitle:title message:message confirmAction:confirmStr cancelAction:cancelStr confirmBlock:confirm cancelBlock:cancel];
}
//显示视图
- (void)show {
//0. 首先把自己添加到 [UIApplication sharedApplication].keyWindow 上
self.frame = self.supView.frame;
[self.supView addSubview:self];
//1. 蒙版
self.backView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.backView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.414];
[self addSubview:self.backView];
//2. 显示的 alertView
self.customAlertView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW - kMargin*3, kScreenH - kMargin *8)];
self.customAlertView.center = self.center;
self.customAlertView.backgroundColor = [UIColor whiteColor];
[self.backView addSubview:self.customAlertView];
self.customAlertView.layer.cornerRadius = 6;
self.customAlertView.layer.masksToBounds = YES;
//2.0 自定义 `提示框` 上的控件
CGFloat customW = CGRectGetWidth(self.customAlertView.frame);
[self.title drawInRect:CGRectMake(0, 0, kScreenW - kMargin*3, kScreenH - kMargin *8) withAttributes:nil];
//2.1 标题 title
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, kAlertViewPadding, customW, 20)];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.text = self.title;
[self.customAlertView addSubview:self.titleLabel];
CGSize size = CGSizeMake(customW - kAlertViewPadding*2, 0);
NSDictionary *dict = @{
NSFontAttributeName: [UIFont systemFontOfSize:15]
};
CGRect frame = [self.message boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dict context:nil];
//2.2 信息 message
CGFloat messageY = CGRectGetMaxY(self.titleLabel.frame) + kAlertViewPadding;
self.messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(kAlertViewPadding, messageY, customW - kAlertViewPadding*2, frame.size.height > 20 ? frame.size.height : 20)];
self.messageLabel.textAlignment = NSTextAlignmentCenter;
self.messageLabel.font = [UIFont systemFontOfSize:13];
self.messageLabel.text = self.message;
self.messageLabel.numberOfLines = 0;
[self.customAlertView addSubview:self.messageLabel];
//2.3 确定 和 取消按钮
CGFloat separatorY = CGRectGetMaxY(self.messageLabel.frame) + kAlertViewPadding*2;
self.segemtedControl = [[UISegmentedControl alloc] initWithItems:@[self.cancelStr, self.confirmStr]];
self.segemtedControl.tintColor = [UIColor grayColor];
self.segemtedControl.frame = CGRectMake(-2, separatorY, customW + 4, 45);
//设置字体属性
NSDictionary *normalDict = @{
NSFontAttributeName: [UIFont systemFontOfSize:14],
NSForegroundColorAttributeName: [UIColor blackColor],
};
//设置正常状态
[self.segemtedControl setTitleTextAttributes:normalDict forState:UIControlStateNormal];
[self.segemtedControl addTarget:self action:@selector(segmentSelect:) forControlEvents:UIControlEventValueChanged];
[self.customAlertView addSubview:self.segemtedControl];
CGFloat selfH = CGRectGetMaxY(self.segemtedControl.frame);
CGRect frame1 = self.customAlertView.frame;
frame1.size.height = selfH - 2;
self.customAlertView.frame = frame1;
//3. 动画显示
[UIView animateWithDuration:0.2 animations:^{
self.customAlertView.center = CGPointMake(self.backView.center.x, self.backView.center.y);
}];
}
#pragma mark - action
- (void)segmentSelect:(UISegmentedControl *)segment {
[UIView animateWithDuration:0.1 animations:^{
self.backView.backgroundColor = [UIColor clearColor];
self.customAlertView.center = self.center;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
switch (segment.selectedSegmentIndex) {
case 0:
self.cancelBlock();
break;
case 1:
self.confirmBlock();
break;
default:
break;
}
}
3 . 调用
ZHZAlertView *view = [ZHZAlertView alertViewWithTitle:@"标题" message:@"一直想学PHP\n一直想学H5\n一直想学PYTHON" confirmAction:@"确定" cancelAction:@"取消" confirmBlock:^{
NSLog(@"确定00000");
} cancelBlock:^{
NSLog(@"取消1111");
}];
[view show];
4 . 一些说明:
首先自定义的这个提示框很简单, 并且扩展性很差, 主要用于二选一的场景;
以后根据项目中的需求 肯定会慢慢完善的;
不过有了这个思路, 再进行扩展就很easy了.