ios开发中自定义UIAlertView;

在iOS 中我们有许多情况下需要用alertView,系统的UIAlertViewUI并不总是符合设计要求,我们需要自定义一个。

在开发中我才用继承UIWindow的方式自定义,UIWinow是屏幕展示UIViewController的桥梁,在APP入口时Appdelegate中我们能看到我们需要设置self.window.rootViewConroller才能看到我们的界面。下面来看代码:

在.h文件中

#import <UIKit/UIKit.h>


@interface customAlertView : UIWindow

-(void)show;

-(void)dismiss;

typedef void (^sureBtnClickBlock)(UIButton *sender);

typedef void (^cancelBtnClickBlock)(UIButton *sender);

@property (nonatomic,copy) sureBtnClickBlock sureBlock;

@property (nonatomic,copy) cancelBtnClickBlock cancelBlock;

@end

我们定义了show和dismiss以及点击和取消的block

在.m中:

#import "customAlertView.h"

#import "loginTextFieldBackView.h"

@implementation customAlertView

{

    UIButton *sureBtn;

    UIButton *cancelBtn;

    UITextField *accountTextField;

    UITextField *pwdTextField;

}

-(id)initWithFrame:(CGRect)frame{

    

    self=[super initWithFrame:frame];

    if(self){

        

        self.windowLevel=UIWindowLevelAlert;

        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(60, 160, kSCALE_WIDTH(270),kSCALE_HEIGHT(188.5))];

        view.backgroundColor = kLogBackGroundColor;

        view.center = CGPointMake(kScreenWidth/2, kScreenHeight/2-30);

        [view.layer setBorderColor:kGetColor(42, 56, 78).CGColor];

        [view.layer setBorderWidth:kOnePointHeight];

        [view.layer setCornerRadius:7.0f];

        [view.layer setMasksToBounds:YES];

    

        

        UILabel *titleLabel=[PublicLabel labelWithFrame:CGRectMake(kSCALE_WIDTH(30), kSCALE_HEIGHT(10), view.frame.size.width-2*kSCALE_WIDTH(30), 20) text:@"账号" textColor:[UIColor whiteColor] font:FONT(13.5) textAlignment:NSTextAlignmentLeft backgroundColor:[UIColor clearColor]];

        [view addSubview:titleLabel];

        

        UIView  *accountView=[[UIView alloc]initWithFrame:CGRectMake(kSCALE_WIDTH(20), CGRectGetMaxY(titleLabel.frame)+5, view.frame.size.width-2*kSCALE_WIDTH(20), kSCALE_HEIGHT(30))];

        [accountView.layer setBorderColor:kGetColor(42, 56, 78).CGColor];

        [accountView.layer setBorderWidth:kOnePointHeight];

        [view addSubview:accountView];

        

        accountTextField=[[PublicTextfield alloc]initWithFrame:CGRectMake(10, 0, accountView.frame.size.width-20, accountView.frame.size.height)];

        [accountView addSubview:accountTextField];

        [accountTextField setFont:FONT(13.5)];

        [accountTextField setTextColor:kGetColor(92, 102, 114)];

        [accountTextField setBackgroundColor:[UIColor clearColor]];

        [accountTextField setAttributedPlaceholder:[self setMyPlaceHolder:@"请输入账号"]];

        

        

        

        

        

        UILabel *pwdLabel=[PublicLabel labelWithFrame:CGRectMake(kSCALE_WIDTH(30), CGRectGetMaxY(accountView.frame)+kSCALE_HEIGHT(10), view.frame.size.width-2*kSCALE_WIDTH(30), 20) text:@"密码" textColor:[UIColor whiteColor] font:FONT(13.5) textAlignment:NSTextAlignmentLeft backgroundColor:[UIColor clearColor]];

        [view addSubview:pwdLabel];

        

        UIView *pwdView=[[UIView alloc]initWithFrame:CGRectMake(kSCALE_WIDTH(20), CGRectGetMaxY(pwdLabel.frame)+5, view.frame.size.width-2*kSCALE_WIDTH(20), kSCALE_HEIGHT(30))];

        [pwdView.layer setBorderColor:kGetColor(42, 56, 78).CGColor];

        [pwdView.layer setBorderWidth:kOnePointHeight];

        [view addSubview:pwdView];

        

        pwdTextField=[[PublicTextfield alloc]initWithFrame:CGRectMake(10, 0, pwdView.frame.size.width-20, pwdView.frame.size.height)];

        [pwdView addSubview:pwdTextField];

        [pwdTextField setFont:FONT(13.5)];

        [pwdTextField setTextColor:kGetColor(92, 102, 114)];

        [pwdTextField setBackgroundColor:[UIColor clearColor]];

        [pwdTextField setAttributedPlaceholder:[self setMyPlaceHolder:@"请输入密码"]];

        


        UIView *bottomLine=[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(pwdView.frame)+kSCALE_HEIGHT(15), view.frame.size.width, kOnePointHeight)];

        [bottomLine setBackgroundColor:kGetColor(42, 56, 78)];

        [view addSubview:bottomLine];

        

        UIView *verLine=[[UIView alloc]initWithFrame:CGRectMake(view.frame.size.width/2-kOnePointHeight/2, CGRectGetMaxY(bottomLine.frame), kOnePointHeight, view.frame.size.height-CGRectGetMaxY(bottomLine.frame))];

        [verLine setBackgroundColor:kGetColor(42, 56, 78)];

        [view addSubview:verLine];

        

        

       

        

        sureBtn=[PublicButton addNewButtonWithRect:CGRectMake(0, CGRectGetMaxY(bottomLine.frame), view.frame.size.width/2, verLine.frame.size.height) Title:@"确认" FontSize:13.5 TitleColor:kGetColor(94, 120, 160) Target:self action:@selector(sureBtnClick:) backGroundCorlor:nil];

        [view addSubview:sureBtn];

        

        

        cancelBtn=[PublicButton addNewButtonWithRect:CGRectMake(CGRectGetMaxX(verLine.frame), CGRectGetMaxY(bottomLine.frame), view.frame.size.width/2, verLine.frame.size.height) Title:@"取消" FontSize:13.5 TitleColor:kGetColor(94, 120, 160) Target:self action:@selector(cancelBtnClick:) backGroundCorlor:nil];

        [view addSubview:cancelBtn];

        

        [self addSubview:view];

        

    }

    return self;

}


-(void)sureBtnClick:(id)sender

{

    self.sureBlock(sender);

  

}

-(void)cancelBtnClick:(id)sender{

    

    self.cancelBlock(sender);

}

-(void)show{

    

    [self makeKeyAndVisible];

}

-(void)dismiss{

    

    [self setHidden:YES];

    [self resignKeyWindow];

}

其中view为自定义view在上面我们可以自己修改想要的效果如图:






demo地址:https://github.com/zhoutianrui/custormAlertView

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值