美文网首页UI基础
iOS-自定义AlertView

iOS-自定义AlertView

作者: OlivierZhang | 来源:发表于2016-03-09 16:31 被阅读836次

使用工厂方法创建2种类型的自定义AlertView。

效果图:


类似系统的AlertView 竖直菜单栏

在.h文件中实现:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@protocol OZAlertViewDelegate <NSObject>

@optional
// 点击按钮下标时传递参数
- (void)didSelectVerticalAlertButton:(NSString *)title;
- (void)didSelectHorizontalAlertButton:(NSString *)title;
@end

@interface OZAlertView : NSObject

@property (nonatomic,weak) id <OZAlertViewDelegate> delegate;

/**
 *单例
 */
+ (instancetype)shareInstance;

/**
 *快速竖直创建提示框
 */
- (UIView *)quickVerticalAlertViewWithArray:(NSArray *)array;

/**
 *快速横向创建提示框
 */
- (UIView *)quickHorizontalAlertViewTitle:(NSString *)title Content:(NSString *)content ButtonTitles:(NSArray *)buttonTitles;
@end

在.m文件中实现

#import "OZAlertView.h"
#import "OZHelper.h"

@implementation OZAlertView

+ (instancetype)shareInstance {
    static OZAlertView *alert;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        alert = [[OZAlertView alloc] init];
    });
    return alert;
}

- (UIView *)quickVerticalAlertViewWithArray:(NSArray *)array {
    
    UIView *groundView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    groundView.alpha = 0;
    groundView.hidden = YES;
    groundView.backgroundColor = [UIColor clearColor];
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    backgroundView.backgroundColor = [UIColor blackColor];
    backgroundView.alpha = 0.5;
    [groundView addSubview:backgroundView];
    
    CGFloat buttonH = 61;
    CGFloat buttonW = 250;
    
    // 通过数组长度创建view的高
    UIView *alert = [[UIView alloc] initWithFrame:CGRectMake(0, 0,buttonW, array.count*buttonH)];
    // 切圆角
    alert.layer.masksToBounds = YES;
    alert.layer.cornerRadius = 10;
    alert.center = groundView.center;
    [groundView addSubview:alert];
    
    for (int i = 0; i < array.count; i++) {
        // 因为有一条分割线 所以最下面是一层view
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, i*buttonH, buttonW, buttonH)];
        view.backgroundColor = [UIColor whiteColor];
        
        //创建button
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(0, 0, buttonW, buttonH);
        [button setTitle:array[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:17];
        
        [button addTarget:self action:@selector(verticalAlertAction:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
        
        CGFloat height = [OZHelper heightOfString:array[0] font:[UIFont systemFontOfSize:17] width:buttonW - 20];
        
        //设置修改Frame
        if (height > buttonH) {
            if (i == 0) {
                button.frame = CGRectMake(10, 10, buttonW - 20, height);
                
                CGRect viewFrame = view.frame;
                viewFrame.size.height = 20 + height;
                view.frame = viewFrame;
                
                CGRect alertFrame = alert.frame;
                alertFrame.size.height += view.frame.size.height - buttonH;
                alert.frame = alertFrame;
            }
            else
            {
                CGRect viewFrame = view.frame;
                viewFrame.origin.y = 20 + height;
                view.frame = viewFrame;
            }
        }
        
        //设置第一行不能点击
        if (i == 0) {
            button.userInteractionEnabled = NO;
            button.titleLabel.numberOfLines = 0;
        }
        
        //设置颜色分割线
        if (i == array.count - 1) {
            button.tintColor = [UIColor whiteColor];
            // 背景
            view.backgroundColor = [UIColor purpleColor];
        }
        else {
            button.tintColor = [UIColor purpleColor];
            // 分割线
            // 如果不是最后一行
            UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 60, buttonW, 1)];
            lineView.backgroundColor = [UIColor purpleColor];
            [view addSubview:lineView];
        }
        [alert addSubview:view];
    }
    
    return groundView;
}

- (UIView *)quickHorizontalAlertViewTitle:(NSString *)title Content:(NSString *)content ButtonTitles:(NSArray *)buttonTitles
{
    UIView *groundView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    groundView.alpha = 0;
    groundView.hidden = YES;
    groundView.backgroundColor = [UIColor clearColor];
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    backgroundView.backgroundColor = [UIColor blackColor];
    backgroundView.alpha = 0.5;
    [groundView addSubview:backgroundView];
    
    CGFloat buttonH = 61;
    CGFloat buttonW = 125;
    
    CGFloat height = [OZHelper heightOfString:content font:[UIFont systemFontOfSize:15] width:2*buttonW - 20] + 23;
    
    // 通过数组设置创建view的高
    CGFloat alertHeight = height > buttonH ? 2*buttonH + height - buttonH + 20 : 2*buttonH;
    UIView *alert = [[UIView alloc] initWithFrame:CGRectMake(0, 0,2*buttonW, alertHeight)];
    // 设置圆角
    alert.layer.masksToBounds = YES;
    alert.layer.cornerRadius = 10;
    alert.center = groundView.center;
    [groundView addSubview:alert];
    
    for (int i = 0; i < 2; i++) {
        // 因为有一条分割线 所以最下面是一层view
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor whiteColor];
        
        if (i == 0) {
            
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 2*buttonW - 20, 20)];
            titleLabel.text = title;
            titleLabel.textAlignment = NSTextAlignmentCenter;
            titleLabel.textColor = [UIColor purpleColor];
            titleLabel.font = [UIFont boldSystemFontOfSize:17];
            titleLabel.backgroundColor = [UIColor whiteColor];
            [view addSubview:titleLabel];
            
            UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(titleLabel.frame), 2*buttonW - 20, height)];
            contentLabel.text = content;
            contentLabel.font = [UIFont systemFontOfSize:15];
            contentLabel.textColor = [UIColor purpleColor];
            contentLabel.numberOfLines = 0;
            contentLabel.backgroundColor = [UIColor whiteColor];
            [view addSubview:contentLabel];
            
            view.frame = CGRectMake(0, 0, 2*buttonW, CGRectGetMaxY(contentLabel.frame)+3);
        }
        else
        {
            for (int j = 0; j < buttonTitles.count; j++) {
                //创建button
                UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
                button.frame = CGRectMake(j*buttonW, 0, buttonW, buttonH);
                [button setTitle:buttonTitles[j] forState:UIControlStateNormal];
                button.titleLabel.font = [UIFont systemFontOfSize:17];
                [button addTarget:self action:@selector(horizontalAlertAction:) forControlEvents:UIControlEventTouchUpInside];
                [view addSubview:button];
                
                // 这里可以根据传值改变状态
                if (j == 0) {
                    button.tintColor = [UIColor whiteColor];
                    button.backgroundColor = [UIColor purpleColor];
                }
                else {
                    button.tintColor = [UIColor purpleColor];

                    // 分割线
                    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(buttonW, 0, buttonW, 1)];
                    lineView.backgroundColor = [UIColor purpleColor];
                    [view addSubview:lineView];
                }
            }
            
            view.frame = CGRectMake(0, height>buttonH?height:buttonH, 2*buttonW, buttonH);
        }
        [alert addSubview:view];
    }
    return groundView;
}

- (void)verticalAlertAction:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(didSelectVerticalAlertButton:)]) {
        [self.delegate didSelectVerticalAlertButton:sender.currentTitle];
    }
}

- (void)horizontalAlertAction:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(didSelectHorizontalAlertButton:)]) {
        [self.delegate didSelectHorizontalAlertButton:sender.currentTitle];
    }
}

@end

然后在ViewController中实现:

#import "ViewController.h"
#import "OZAlertView.h"

static NSString *const Title = @"Hellow World~";
static NSString *const content = @"Hellow World~Hellow World~";
static NSString *const btnTitle1 = @"btn1";
static NSString *const btnTitle2 = @"btn2";
static NSString *const btnTitle3 = @"btn3";

@interface ViewController () <OZAlertViewDelegate>
@property (nonatomic, strong) UIView *alert;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //[self createOZHorizontalAlertView];
    [self createOZVerticalAlertView];
    [self alertViewShow];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - OZAlertViewDelegate
- (void)didSelectHorizontalAlertButton:(NSString *)title {
    [self alertViewHide];
}

- (void)didSelectVerticalAlertButton:(NSString *)title {
    [self alertViewHide];
}

#pragma mark -
- (void)createOZHorizontalAlertView {
    self.alert = [[OZAlertView shareInstance] quickHorizontalAlertViewTitle:Title
                                                                    Content:content
                                                               ButtonTitles:@[btnTitle1,btnTitle2]];
    [self.view addSubview:self.alert];
}

- (void)createOZVerticalAlertView {
    self.alert = [[OZAlertView shareInstance] quickVerticalAlertViewWithArray:@[content,btnTitle1,btnTitle2,btnTitle3]];
    
    [self.view addSubview:self.alert];
}

- (void)alertViewShow {
    self.alert.hidden = NO;
    [UIView animateWithDuration:0.35 animations:^{
        self.alert.alpha = 1;
    }];
}

- (void)alertViewHide {
    [UIView animateWithDuration:0.35 animations:^{
        self.alert.alpha = 0;
    } completion:^(BOOL finished) {
        self.alert.hidden = YES;
        [self.alert removeFromSuperview];
    }];
}

@end

Demo地址:https://github.com/olivierzh/OZAlertView.git

相关文章

  • iOS-自定义AlertView

    使用工厂方法创建2种类型的自定义AlertView。 效果图: 在.h文件中实现: 在.m文件中实现 然后在Vie...

  • 自定义AlertView

    自定义AlertView 之囧事 昨天被 AlertView、AlertController虐了 ...然鹅发现...

  • iOS-自定义AlertView(继承UIView)

    特别感谢:誰的青春卟迷茫 继承UIView 可自定义控件,可自行添加控件,可设置控件背景图片,圆角,支持多行信息自...

  • iOS-自定义alertView(非常实用)

  • IOS 日常随笔

    自己的demo 自定义Alertview -- LSAlertView图片浏览器 -- LSShowImgs倒...

  • 自定义带输入框类型AlertView

    由于系统自带AlertView带输入框样式不太美观,决定自定义类似系统带有输入框样式AlertView,可根据需求...

  • iOS-自定义AlertView(便利构造器)

    前言 遍历构造器又称工厂方法,可以把一个或多个控件封装到一个类中,每次创建控件只需要调用方法就可以了 本次我所说的...

  • 自定义AlertView

    CustomAlertView 一个自定义的AlertView,用户可以根据自己的需求来设置。 使用方法 类似于系...

  • 自定义alertview

    .h .m

  • 自定义AlertView

    公司有很多的地方需要用到弹出的提示界面,基本的效果是一样的,但是内容又有很大的不同,每个界面都要重写,工作起来很费...

网友评论

  • 1171cf15abc4:需要在ViewController 里面createOZVerticalAlertView 的时候加上
    [OZAlertView shareInstance].delegate = self ;
    OlivierZhang:@MBOK 是的,我漏掉了 :smile:

本文标题:iOS-自定义AlertView

本文链接:https://www.haomeiwen.com/subject/mzttlttx.html