iOS自定义alertView提示框

iOS UIKit框架下自带的UIAlertView在功能或者美观上,有的时候并不能满足我们的开发需求,这个时候,就可以自定义alertView。我在这里写了一个简单的自定义alertView的Demo,希望能帮助到iOS开发的小伙们。话不多说,直接上代码:

在。h里:

#import <UIKit/UIKit.h>

typedef void(^alertClick)(NSInteger index);

@interface SLAlertView : UIView
@property (nonatomic,copy) alertClick clickIndex;

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message sureBtn:(NSString *)sureTitle cancleBtn:(NSString *)cancleTitle;

- (void)show;

@end


在。m里:

#import "SLAlertView.h"
#define alertViewWidth 280 //alertView 宽
#define edge 10.0 //各个栏目之间的距离

@interface SLAlertView ()
@property (nonatomic,retain) UIView *alertView;//弹窗
@property (nonatomic,retain) UILabel *titleLbl;//title
@property (nonatomic,retain) UILabel *msgLbl;//内容
@property (nonatomic,retain) UIButton *sureBtn;//确认按钮
@property (nonatomic,retain) UIButton *cancleBtn;//取消按钮
@property (nonatomic,retain) UIView *lineView;//横线线
@property (nonatomic,retain) UIView *verLineView;//竖线
@end

@implementation SLAlertView

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message sureBtn:(NSString *)sureTitle cancleBtn:(NSString *)cancleTitle
{
    if (self == [super init]) {
        
        self.frame = [UIScreen mainScreen].bounds;
        
        self.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.6];
        
        self.alertView = [[UIView alloc] init];
        self.alertView.backgroundColor = [UIColor whiteColor];
        self.alertView.layer.cornerRadius = 8.0;
        
        self.alertView.frame = CGRectMake(0, 0, alertViewWidth, 100);
        self.alertView.layer.position = self.center;
        
        if (title) {
            
            self.titleLbl = [self GetAdaptiveLable:CGRectMake(2*edge, 2*edge, alertViewWidth-4*edge, 20) AndText:title andIsTitle:YES];
            self.titleLbl.textAlignment = NSTextAlignmentCenter;
            
            [self.alertView addSubview:self.titleLbl];
            
            CGFloat titleW = self.titleLbl.bounds.size.width;
            CGFloat titleH = self.titleLbl.bounds.size.height;
            
            self.titleLbl.frame = CGRectMake((alertViewWidth-titleW)/2, 2*edge, titleW, titleH);
            
        }
        if (message) {
            
            self.msgLbl = [self GetAdaptiveLable:CGRectMake(edge, CGRectGetMaxY(self.titleLbl.frame)+edge, alertViewWidth-2*edge, 20) AndText:message andIsTitle:NO];
            self.msgLbl.textAlignment = NSTextAlignmentCenter;
            
            [self.alertView addSubview:self.msgLbl];

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值