您的位置:首页 > 产品设计 > UI/UE

《iOS开发笔记—自定义UIAlertController》

2017-05-24 16:42 253 查看
一、自定义UIAlertController

UIAlertController基本可以简单的理解为UIAlertController == UIAlertView + UIActionSheet。

下面我们就自己来定义一个UIAlertController类。

该分类实现了遮盖住UIAlertController 的 视图。

1、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];

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
2、分类使用
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: