自定义alertView


title : 自定义alertView
category : 常用封装


自定义alertView

标签(空格分隔): 常用封装


由于系统自带的UIAlertController无法自定义textField的frame。所以,常常无法满足我们项目中的需求。以下是工作中遇到的一个需求,将简单封装的一个弹窗分享出来

BXAlertView.h


#import 
@class JLAlertView;

@protocol JLAlertViewDelegate 
/** 确定按钮点击 */
- (void)alertViewDetermineButtonClick:(JLAlertView *)alertView;

@end

@interface JLAlertView : UIView

/** 标题 */
@property (weak, nonatomic) IBOutlet UILabel *title;

/** 转发人 */
@property (weak, nonatomic) IBOutlet UITextField *acceptPerson;

/** textView */
@property (weak, nonatomic) IBOutlet UITextView *textView;

/** 确定按钮 */
@property (weak, nonatomic) IBOutlet UIButton *acceptButton;

/** 代理 */
@property (nonatomic, weak) id delegate;

+ (instancetype)alertView;

+ (void)alertShowAddedTo:(UIView *)view;

@end

BXAlertView.m


#import "JLAlertView.h"

@interface JLAlertView ()

@property (weak, nonatomic) IBOutlet UIView *alertView;

@end

@implementation JLAlertView

+ (instancetype)alertView
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([JLAlertView class]) owner:nil options:nil] lastObject];
}

+ (void)alertShowAddedTo:(UIView *)view
{
    JLAlertView *alertView = [JLAlertView alertView];
    [view addSubview:alertView];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
    }
    return [JLAlertView alertView];
}

- (void)awakeFromNib
{
    self.frame = [UIScreen mainScreen].bounds;
}

/**
 *  点击确定按钮
 */
- (IBAction)sureButtonClick {
    NSLog(@"点击确定按钮");
    
    if ([self.delegate respondsToSelector:@selector(alertViewDetermineButtonClick:)]) {
        [self.delegate alertViewDetermineButtonClick:self];
    }
}

/**
 *  取消按钮点击
 */
- (IBAction)cancelButtonClick {
    NSLog(@"点击了取消按钮");
    [self removeFromSuperview];
}

- (void)layoutSubviews
{
    if (self.acceptPerson.hidden) {
        CGFloat margin = 10;
        CGFloat textViewX = margin;
        CGFloat textViewY = CGRectGetMaxY(self.title.frame) + margin * 2;
        CGFloat textVeiwW = self.alertView.frame.size.width - margin * 2;
        CGFloat textViewH = self.acceptButton.frame.origin.y - textViewY - 10;
        
        self.textView.frame = CGRectMake(textViewX, textViewY, textVeiwW, textViewH);
    }
}

@end

自定义alertView_第1张图片
效果.png

代码下载:
链接: http://pan.baidu.com/s/1o8Acfj0 密码: 8qs4

你可能感兴趣的:(自定义alertView)