ios 自定义UIAlertController

一、效果图

  • 实现:简单粗暴,直接 定义 视图控件,遮盖住UIAlertController 的 视图,就可以 达到自定义效果 了。如图: 其实,弹出菜单 覆盖了三个label控件,我们直接控制label就OK了。
  • 学习点:舍去了for循环 和 KVC查找UIAlertController 显示控件 的 销毁性能的效果,取代的是 不起眼的 覆盖方式实现,和 学会重用分类抽取。
效果图.png

二、分类抽取

.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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、效果图 实现:简单粗暴,直接 定义 视图控件,遮盖住UIAlertController 的 视图,就可以 达到...
    大兵_iOS阅读 711评论 0 0
  • 最近项目需求让做一个界面提醒,样式和系统一样,但是字体,颜色,大小却和系统的不太一样。这里总结一下我的做法,希望能...
    huanghy阅读 18,945评论 5 24
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,245评论 4 61
  • 以下内容为工作笔记: 1 尽量使用double,不使用float,否则带来不可预料的截位问题。 2 Xcode8 ...
    蜂猴阅读 829评论 0 0
  • 阿仔高中住校开始她的独立生活,第一次帮她收拾行李,以后都是她自己收拾。来到她的宿舍,小小的柜格、小小的床上放着大小...
    mimi播报阅读 271评论 3 5