版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27339239/article/details/54630009
最近一直忙着做项目,时间紧任务重,客户比较任性,要求做一些比较个性的话的东西,关于alert一类的弹出框,客户的设计师觉得iOS系统的alert样式过于丑陋,强烈要求换成的他的设计。
从iOS 8开始,新加入的了UIAlertController中,
在过去还是UIAlertView的年代,自定义最简单的方法就是写一个View放到Window上,或者加在UIAlertView上面。最开始,我是选着写一个View加载Windows上面,但是由于旧工程里面的代码问题,视图会偶发的不响应点击事件,这就很尴尬了,一个弹出框不能消失,迫于这个工程太过于古老,很多东西现在看来都觉得冗余和不合理。就是简单给加项目功能,就不希望去过多修改过去的代码。这里最终选择的方式就是自定义的View加到UIAlertController上面去。
首先,看看UIAlertController有什么公开的属性和方法,
NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController
+ (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;
- (void)addAction:(UIAlertAction *)action;
@property (nonatomic, readonly) NSArray<UIAlertAction *> *actions;
@property (nonatomic, strong, nullable) UIAlertAction *preferredAction NS_AVAILABLE_IOS(9_0);
- (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler;
@property (nullable, nonatomic, readonly) NSArray<UITextField *> *textFields;
@property (nullable, nonatomic, copy) NSString *title;
@property (nullable, nonatomic, copy) NSString *message;
@property (nonatomic, readonly) UIAlertControllerStyle preferredStyle;
@end
官方公开的属性,并不多,貌似可以定制的东西很有限,但是,我们不能忽略的是UIAlertController是继承与UIViewController的,我们很容易联想到UIViewController的一些特征和属性,最简单的一点就是UIViewController有一个View,我们可以给View加东西。 既然如此,那就去加东西。
代码如下:
//
// QYJCustomAlertViewController.h
// CustomAlertController
//
// Created by Isoftstone on 17/1/20.
// Copyright © 2017年 qyji. All rights reserved.
//
#import <UIKit/UIKit.h>
//点击时间的回调
typedef void(^CustomAlertDismissBlock)(UIButton *button);
@interface QYJCustomAlertViewController : UIAlertController
@property (nonatomic, weak) CustomAlertDismissBlock dismissBlock;
+ (UIAlertController *)showQyjCustomerAlertViewControllerWithSuperController:(UIViewController *)superController actionBlock:(CustomAlertDismissBlock)customBlock;
@end
在.h文件里面放出允许外部操作的属性和方法,如果一个工程里面中弹出框,可以加一个枚举;
//
// QYJCustomAlertViewController.m
// CustomAlertController
//
// Created by Isoftstone on 17/1/20.
// Copyright © 2017年 qyji. All rights reserved.
//
#import "QYJCustomAlertViewController.h"
@interface QYJCustomAlertViewController ()
//用于覆盖原有的样式
@property (nonatomic, strong) UIView *customView;
//一个用于取消的按钮
@property (nonatomic, strong) UIButton *button;
@end
@implementation QYJCustomAlertViewController
#pragma mark - Lifecycle
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//在viewDidLoad里面有加载的self.view的bounds是屏幕的大小,
//所以要在他即将显示的时候去加载控件,如果想在viewDidLoad中加载,就必须拿到一个准确的size,这里建议使用mesonry,或者autolayout去布局。
[self commonInit];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)commonInit {
//如果自定义需要更大的现实面积,可以使用属性,是否裁剪超出super view的部分
self.view.clipsToBounds = NO;
//用于覆盖原有的样式
self.customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
self.customView.backgroundColor = [UIColor redColor];
self.customView.center = self.view.center;
[self.view addSubview:self.customView];
//用于出发隐藏的按钮
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
self.button.frame = CGRectMake(0, 0, 120, 30);
self.button.center = self.customView.center;
self.button.layer.cornerRadius = 5.f;
[self.button setBackgroundColor:[UIColor blueColor]];
[self.button setTitle:@"点击我就消失了" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.customView addSubview:self.button];
}
#pragma mark - Action
- (void)buttonAction:(UIButton *)button {
if (self.dismissBlock) {
_dismissBlock(button);
[self dismissViewControllerAnimated:YES completion:nil];
}
}
#pragma mark - Custom Accessors
#pragma mark - IBActions
#pragma mark - Public
+ (UIAlertController *)showQyjCustomerAlertViewControllerWithSuperController:(UIViewController *)superController actionBlock:(CustomAlertDismissBlock)customBlock;{
//创建一个你自定义的alert,设置弹出的类型 \n 可以控制alertController的大小,最多是5行
QYJCustomAlertViewController *alertController = [QYJCustomAlertViewController alertControllerWithTitle:nil message:@"\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleAlert];
alertController.dismissBlock = customBlock;
//必须加入一个alertAction,否则系统提示你,alertControllrt要有一个一个action
UIAlertAction *action = [UIAlertAction actionWithTitle:@" " style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:action];
//弹出alertView
[superController presentViewController:alertController animated:YES completion:nil];
return alertController;
}
#pragma mark - Private
#pragma mark - NSCopying
#pragma mark - other
@end
这样一个简单的自定义alertController就做完了,这里弹出和消失都动画效果,像要圆角可以直接设置。