2021-01-22

2021-01-22  本文已影响0人  白色天空729

iOS自定义弹出视图

CustomAlertView.h

#import <UIKit/UIKit.h>
@interface CustomAlertView : UIView

- (id)initWithTitle:(NSString *)title message:(id)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle sureButtonTitle:(NSString *)sureButtonTitle;

- (void)show;

- (void)dismiss;

@end

@protocol CustomAlertViewDelegate <NSObject>

@optional
- (void)customAlertView:(CustomAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

@end

CustomAlertView.m


#import "CustomAlertView.h"

#define UIColorRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define MainTextColor   UIColorRGB(0x333333)//用于重要文字信息,内页文字标题
#define AssistTextColor UIColorRGB(0xb1b1b1)//用于普通文字段落,引导词
#define LineColor       UIColorRGB(0xe5e5e5)//分割线颜色
#define BlueColor        UIColorRGB(0x1F9AED)//红色

#define AlertWidth          272.0
#define ButtonHeight        44

@interface CustomAlertView () {
    UIWindow *_alertWindow;
    UILabel *_titleLabel;
    UILabel *_messageLabel;
}

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

@end

@implementation CustomAlertView

- (id)initWithTitle:(NSString *)title message:(id)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle sureButtonTitle:(NSString *)sureButtonTitle {
    CGRect frame = CGRectMake(24, 0, AlertWidth, 100);
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColorRGB(0xfafafa);
        self.layer.cornerRadius = 4;
        self.clipsToBounds = YES;
        _delegate = delegate;
        if (title) {
            [self setTitleLabelWithTitle:title];
        }
        if (message) {
            [self setMessageLabelWithMessage:message];
        }
        float Y = 0;
        float height = 0;
        if (_messageLabel) {
            Y = _messageLabel.frame.size.height + _messageLabel.frame.origin.y + 15;
            height = _messageLabel.frame.origin.y + _messageLabel.frame.size.height + ButtonHeight + 15;
        } else {
            Y = 50;
            height = 50 + 44;
        }
        
        [self creatButtonY:Y CancelButtonTitle:cancelButtonTitle sureButtonTitle:sureButtonTitle];
        float selfY = ([UIScreen mainScreen].bounds.size.height - height)/2.0;
        self.frame = CGRectMake(24, selfY, AlertWidth, height);
        
    }
    return self;
}


- (void)setTitleLabelWithTitle:(NSString *)title {
    float X = 10.0;
    float width = AlertWidth - X * 2;
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(X, 15, width, 20)];
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.font = [UIFont systemFontOfSize:17];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.textColor = MainTextColor;
    _titleLabel.text = title;
    [self addSubview:_titleLabel];
}

- (void)setMessageLabelWithMessage:(id)message {
    float X = 10.0;
    float Y = 15;
    if (_titleLabel) {
        Y = Y + _titleLabel.frame.origin.y + _titleLabel.frame.size.height;
    }
    float width = AlertWidth - X * 2.0;
    _messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(X, Y, width, 20)];
    _messageLabel.numberOfLines = 0;
    _messageLabel.backgroundColor = [UIColor clearColor];
    _messageLabel.font = [UIFont systemFontOfSize:15];
    _messageLabel.textColor = AssistTextColor;
    _messageLabel.textAlignment = NSTextAlignmentCenter;
    if ([message isKindOfClass:[NSString class]]) {
        _messageLabel.text = message;
    } else {
        _messageLabel.attributedText = message;
    }
    [_messageLabel sizeToFit];
    CGSize size = _messageLabel.frame.size;
    _messageLabel.frame = CGRectMake((AlertWidth - size.width)/2.0, _messageLabel.frame.origin.y, size.width, size.height);
    [self addSubview:_messageLabel];
}

- (void)creatButtonY:(float)Y CancelButtonTitle:(NSString *)cancleButtonTitle sureButtonTitle:(NSString *)sureButtonTitle {
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, Y, AlertWidth, 1)];
    lineLabel.backgroundColor = LineColor;
    [self addSubview:lineLabel];
    if (cancleButtonTitle && sureButtonTitle) {
        CGRect cancelRect = CGRectMake(0, Y + 1, AlertWidth/2.0 - 0.5, ButtonHeight);
        UIButton *cancelButton = [self customFrame:cancelRect Title:cancleButtonTitle BtnTag:0];
        [self addSubview:cancelButton];
        
        UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(AlertWidth/2.0 - 0.5, Y + 1, 1, ButtonHeight)];
        lineLabel.backgroundColor = LineColor;
        [self addSubview:lineLabel];
        
        CGRect sureRect = CGRectMake(AlertWidth/2.0 + 0.5, Y + 1, AlertWidth/2.0 - 0.5, ButtonHeight);
        UIButton *sureButton = [self customFrame:sureRect Title:sureButtonTitle BtnTag:1];
        [self addSubview:sureButton];
    } else if (cancleButtonTitle) {
        CGRect cancelRect = CGRectMake(0, Y + 1, AlertWidth, ButtonHeight);
        UIButton *cancelButton = [self customFrame:cancelRect Title:cancleButtonTitle BtnTag:0];
        [self addSubview:cancelButton];
    } else if (sureButtonTitle) {
        CGRect sureRect = CGRectMake(0, Y + 1, AlertWidth, ButtonHeight);
        UIButton *sureButton = [self customFrame:sureRect Title:sureButtonTitle BtnTag:1];
        [self addSubview:sureButton];
    }
}

//创建取消或确认按钮
- (UIButton *)customFrame:(CGRect)frame Title:(NSString *)title BtnTag:(NSInteger)tag {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    if (tag == 0) {
        [button setTitleColor:MainTextColor forState:UIControlStateNormal];
    } else if (tag == 1) {
        [button setTitleColor:BlueColor forState:UIControlStateNormal];
    }
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    button.tag = tag;
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

- (void)buttonAction:(UIButton *)button {
    if ([_delegate respondsToSelector:@selector(customAlertView:clickedButtonAtIndex:)]) {
        [_delegate customAlertView:self clickedButtonAtIndex:button.tag];
    }
    [self endEditing:YES];
    [_alertWindow resignFirstResponder];
    [_alertWindow resignKeyWindow];
    _alertWindow.alpha = 0;
    _alertWindow = nil;
}

- (void)dismiss {
    [_alertWindow resignFirstResponder];
    [_alertWindow resignKeyWindow];
    _alertWindow.alpha = 0;
    _alertWindow = nil;
}

- (void)show {
    [self dismiss];
    if (!_alertWindow) {
        _alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _alertWindow.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        _alertWindow.windowLevel = UIWindowLevelNormal;
    }
    [_alertWindow addSubview:self];
    [_alertWindow makeKeyAndVisible];
}

@end


上一篇 下一篇

猜你喜欢

热点阅读