XZ_iOS中textField的键盘弹出和回收导致的界面上移和下移

例如,有一个填写姓名的输入框,当点击姓名下方的输入框时,键盘弹出,界面上移;当点击提交的时候,键盘回收,界面下移;

XZSignUpView.h中

@interface XZSignUpView : UIView

// 点击提交按钮

@property (nonatomic,copy)void(^blockSubmitBtn)(NSString *,NSString *);

@property (nonatomic,copy)void(^blockCloseBtn)(UIButton *);

@end


XZSignUpView.m中

#import "XZSignUpView.h"


@interface XZSignUpView()

/** 姓名输入框 */

@property (nonatomic,strong)UITextField *textName;

/** 背景图 */

@property (nonatomic,strong)UIView *viewBackGround;

/** 手机号输入框 */

@property (nonatomic,strong)UITextField *textPhoneNumber;

/** 提交按钮 */

@property (nonatomic,strong)UIButton *btnSubmit;

@end


@implementation XZSignUpView

- (instancetype)initWithFrame:(CGRect)frame{

    if (self = [superinitWithFrame:frame]) {

        [selfsetUpSignUpView];

       //添加监听,当键盘出现时收到消息

        [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];

       //添加监听,当键盘退出时收到消息

        [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];

    }

    return self;

}


// 当键盘出现或改变时调用

- (void)keyboardWillShow:(NSNotification *)aNotification

{

    // 获取键盘的高度

    NSDictionary *userInfo = [aNotification userInfo];

    NSValue *aValue = [userInfoobjectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [aValue CGRectValue];

    int height = keyboardRect.size.height;

    if (self.textName.text.length ==0) {//键盘弹出

        self.viewBackGround.frame = CGRectMake(0,KProjectScreenHeight-height-300,KProjectScreenWidth,300);

    }else{

        CGRect rect =CGRectMake(0,KProjectScreenHeight -self.viewBackGround.frame.size.height-height,KProjectScreenWidth,self.viewBackGround.frame.size.height);

        self.viewBackGround.frame = rect;

    }

}


// 当键退出时调用

- (void)keyboardWillHide:(NSNotification *)aNotification

{

    if (self.textName.text.length ==0) {

        self.viewBackGround.frame = CGRectMake(0,KProjectScreenHeight-300,KProjectScreenWidth,300);

    }else{

        CGRect rect =CGRectMake(0,KProjectScreenHeight -self.viewBackGround.frame.size.height,KProjectScreenWidth,self.viewBackGround.frame.size.height);

        self.viewBackGround.frame = rect;

    }

}


- (void)setUpSignUpView {

    /** 背景图 */

    UIView *viewBackGround = [[UIViewalloc]init];

    [self addSubview:viewBackGround];

    [viewBackGround mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(self.mas_left);

        make.right.equalTo(self.mas_right);

        make.bottom.equalTo(self.mas_bottom);

        make.height.equalTo(@300);

    }];

    self.viewBackGround = viewBackGround;

    viewBackGround.backgroundColor = [UIColorwhiteColor];

    

    /** 关闭按钮 */

    UIButton *btnClose = [UIButtonbuttonWithType:UIButtonTypeSystem];

    [viewBackGround addSubview:btnClose];

    [btnClose mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(viewBackGround.mas_top).offset(20);

        make.right.equalTo(viewBackGround.mas_right).offset(-20);

        make.height.and.width.equalTo(@18);

    }];

    [btnClose addTarget:selfaction:@selector(didClickCloseButton:)forControlEvents:UIControlEventTouchUpInside];

    [btnClose setBackgroundImage:[UIImageimageNamed:@"签到关闭icon_03"]forState:UIControlStateNormal];

    

    /** 姓名 */

    UILabel *labelName = [[UILabelalloc]init];

    [viewBackGround addSubview:labelName];

    [labelName mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(self.mas_left).offset(20);

        make.top.equalTo(btnClose.mas_bottom).offset(10);

    }];

    labelName.text = @"姓名";

    

    /** 姓名输入框 */

    UITextField *textName = [[UITextFieldalloc]init];

    [viewBackGround addSubview:textName];

    [textName mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(labelName.mas_left);

        make.top.equalTo(labelName.mas_bottom).offset(5);

        make.right.equalTo(btnClose.mas_right);

        make.height.equalTo(@45);

    }];

    self.textName = textName;

    textName.borderStyle =UITextBorderStyleRoundedRect;

    

    /** 手机号 */

    UILabel *labelPhoneNumber = [[UILabelalloc]init];

    [viewBackGround addSubview:labelPhoneNumber];

    [labelPhoneNumber mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(labelName.mas_left);

        make.top.equalTo(textName.mas_bottom).offset(20);

    }];

    labelPhoneNumber.text = @"手机号";

    

    /** 手机号输入框 */

    UITextField *textPhoneNumber = [[UITextFieldalloc]init];

    [viewBackGround addSubview:textPhoneNumber];

    [textPhoneNumber mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(labelName.mas_left);

        make.top.equalTo(labelPhoneNumber.mas_bottom).offset(5);

        make.right.equalTo(btnClose.mas_right);

        make.height.equalTo(textName.mas_height);

    }];

    self.textPhoneNumber = textPhoneNumber;

    textPhoneNumber.borderStyle =UITextBorderStyleRoundedRect;

    

    /** 提交按钮 */

    UIButton *btnSubmit = [UIButtonbuttonWithType:UIButtonTypeCustom];

    [viewBackGround addSubview:btnSubmit];

    [btnSubmit mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(textPhoneNumber.mas_left);

        make.bottom.equalTo(self.mas_bottom).offset(-15);

        make.right.equalTo(textPhoneNumber.mas_right);

        make.height.equalTo(textName.mas_height);

    }];

    self.btnSubmit = btnSubmit;

    [btnSubmit addTarget:selfaction:@selector(didClickSubmitButton:)forControlEvents:UIControlEventTouchUpInside];

    [btnSubmit.titleLabelsetFont:[UIFontsystemFontOfSize:17weight:2]];

    [btnSubmit setBackgroundColor:XZColor(14,93,210)];

    btnSubmit.layer.masksToBounds =YES;

    btnSubmit.layer.cornerRadius =5.0f;

    // 

}


// 点击关闭按钮

- (void)didClickCloseButton:(UIButton *)button {

    [selfremoveFromSuperview];

    if (self.blockCloseBtn) {

        self.blockCloseBtn(button);

    }

}


// 点击提交按钮

- (void)didClickSubmitButton:(UIButton *)button {

    [selfendEditing:YES];

    if (self.blockSubmitBtn) {

        self.blockSubmitBtn(self.textName.text,self.textPhoneNumber.text);

    }

    NSLog(@"%@,%@",self.textName.text,self.textPhoneNumber.text);

    // 发送成功之后清空

//    self.textName.text = nil;

//    self.textPhoneNumber.text = nil;

    self.viewBackGround.frame = CGRectMake(0,KProjectScreenHeight-300,KProjectScreenWidth,300);

   

}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [selfendEditing:YES];

}


@end



// XZSignUpView的使用

- (XZSignUpView *)signUpView {

    if (!_signUpView) {

        _signUpView = [[XZSignUpViewalloc]initWithFrame:[UIScreenmainScreen].bounds];

        __weak __typeof(&*self)weakSelf =self;

        _signUpView.btnTitle =@"提交";

        _signUpView.blockCloseBtn = ^(UIButton *button) {

            [weakSelf.viewBackGround removeFromSuperview];

        };

    }

    return_signUpView;

}


UIWindow *keyWindow = [UIApplicationsharedApplication].keyWindow;

    UIView *viewBackGround = [[UIViewalloc]initWithFrame:keyWindow.frame];

    viewBackGround.backgroundColor = [UIColorblackColor];

    viewBackGround.alpha = 0.6;

[keyWindow addSubview:self.signUpView];




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值