一、效果图
- 实现:简单粗暴,直接 定义 视图控件,遮盖住UIAlertController 的 视图,就可以 达到自定义效果 了。如图: 其实,弹出菜单 覆盖了三个label控件,我们直接控制label就OK了。
- 学习点:舍去了for循环 和 KVC查找UIAlertController 显示控件 的 销毁性能的效果,取代的是 不起眼的 覆盖方式实现,和 学会重用分类抽取。
二、分类抽取
.h文件
#import <UIKit/UIKit.h>
@interface UIAlertController (category)
- (void)configTitles: (NSArray *)titles withActionHandlers:(NSArray *)actionHandlers;
@end
.m文件
#import "UIAlertController+category.h"
#import <objc/runtime.h>
static void * containerViewPorpertyKey = (void *)@"containerViewPorpertyKey";
CGFloat padding = 10;
CGFloat itemHeight = 57;
CGFloat lineHeight = 0.5;
CGFloat itemCount = 2;
@interface UIAlertController ()
@property (nonatomic, retain) UIView *containerView;
@end
@implementation UIAlertController (category)
- (id)containerView // containerView
{
return objc_getAssociatedObject(self, containerViewPorpertyKey);
}
- (void)setContainerView:(id)containerView
{
objc_setAssociatedObject(self, containerViewPorpertyKey, containerView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)configTitles: (NSArray *)titles withActionHandlers:(NSArray *)actionHandlers{
// 视图
CGFloat alertVCWidth = self.view.frame.size.width - 2 * padding;
self.containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, alertVCWidth, itemHeight*itemCount + (itemCount-1)*lineHeight)];
[self.view addSubview: self.containerView];
// x y w h
// padding 0 alertVCWidth - 2 * padding, h
// padding (h+lineH)*1 alertVCWidth - 2 * padding, h
// padding (h+lineH)*2 alertVCWidth - 2 * padding, h
if (titles.count-1 > 0) {
for (int i = 0; i< titles.count-1; i++) {
UILabel *l = [[UILabel alloc] init];
l.frame = CGRectMake(padding, (itemHeight+lineHeight)*i, alertVCWidth - 2 * padding, itemHeight);
l.backgroundColor = [UIColor clearColor];
l.text = titles[i];
l.font = [UIFont systemFontOfSize:30];
l.textAlignment = NSTextAlignmentCenter;
l.textColor = [UIColor blackColor];
l.userInteractionEnabled = false;
[self.containerView addSubview: l];
}
}
// actions
if (actionHandlers.count-1 > 0) {
for (int i = 0; i< actionHandlers.count-1; i++) {
UIAlertAction *action = [UIAlertAction actionWithTitle:@"" style:UIAlertActionStyleDefault handler: actionHandlers[i]];
[self addAction: action];
}
}
// 取消
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:titles[titles.count-1] style:UIAlertActionStyleCancel handler: actionHandlers[actionHandlers.count-1]];
[self addAction:cancelAction];
}
@end
三、分类使用
#import "ViewController.h"
#import "UIAlertController+category.h"
@interface ViewController ()
@property(nonatomic, retain) UIAlertController *alertVC;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
NSArray *titles = @[@"保存", @"保存222", @"123", @"取消"];
NSArray *actionHandles = @[
^(UIAlertAction *action){
NSLog(@"1");
},
^(UIAlertAction *action){
NSLog(@"2");
},
^(UIAlertAction *action){
NSLog(@"3");
},
^(UIAlertAction *action){
NSLog(@"取消");
}
];
[self.alertVC configTitles:titles withActionHandlers:actionHandles];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self presentViewController:self.alertVC animated:YES completion:nil];
}
@end