iOS CJKeyBoardBarTextField_键盘遮挡处理

IOS开发中, 键盘遮挡输入框, 肯定经常遇到, 平时通常做法是让view上移, 然后个人感觉比较麻烦, 自己自定义了一个textfiled来解决, 如果大家需要可以借鉴


效果

这里写图片描述


使用方法

在xib中直接指定类型
这里写图片描述

或者直接代码创建


代码

//
//  JJKeyboardBarTextField.m
//  employer
//
//  Created by corey_jia on 15/11/12.
//  Copyright © 2015年 corey_jia All rights reserved.
//

#import "CJKeyboardBarTextField.h"

@interface CJKeyboardBarTextField ()

@property (nonatomic,weak) UITextField *textField;
@property (nonatomic,strong) UIButton *returnBtn;
@property (nonatomic,strong) UIView *controllerView;
@property (nonatomic,strong) UIView *toolBar;
@property (nonatomic,strong) UITextField *toolBarTextField;

@end

@implementation CJKeyboardBarTextField

#define fieldPadding    10
#define toolbarHeight   38
#define returnBtnWidth  50
#define toolBarTextFieldTopPadding  4

- (UITextField *)toolBarTextField{
    if (!_toolBarTextField) {
        _toolBarTextField = [[UITextField alloc]initWithFrame:CGRectMake(fieldPadding, toolBarTextFieldTopPadding,self.controllerView.frame.size.width-self.returnBtn.frame.size.width - fieldPadding*3, toolbarHeight-8)];
        _toolBarTextField.backgroundColor = [UIColor whiteColor];
        _toolBarTextField.borderStyle = UITextBorderStyleRoundedRect;
        _toolBarTextField.keyboardType = self.keyboardType;
        _toolBarTextField.secureTextEntry = self.secureTextEntry;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChanged) name:UITextFieldTextDidChangeNotification object:_toolBarTextField];
    }
    return _toolBarTextField;
}

- (UIView *)toolBar{
    if (!_toolBar) {
        _toolBar = [[UIView alloc]initWithFrame:CGRectMake(0, self.controllerView.frame.size.height, self.controllerView.frame.size.width, toolbarHeight)];
        _toolBar.backgroundColor = [UIColor whiteColor];
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _toolBar.frame.size.width, 0.5)];
        line.backgroundColor = [UIColor lightGrayColor];
        [_toolBar addSubview:line];
    }
    return _toolBar;
}

- (UIView *)controllerView{
    if (!_controllerView) {
        UIViewController *vc = [self viewController];
        _controllerView = vc.view;
    }
    return _controllerView;
}

- (UIButton *)returnBtn{
    if (!_returnBtn) {
        _returnBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.controllerView.frame.size.width - returnBtnWidth - fieldPadding, 0, 50, toolbarHeight)];
        [_returnBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_returnBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [_returnBtn addTarget:self action:@selector(returnBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _returnBtn;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setup];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

}

- (void)done {
    [self resignFirstResponder];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    if (![self isFirstResponder]) return;

    NSDictionary *info = [notification userInfo];
    CGRect kbFrame = [[info valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect newKbFrame = [self.window convertRect:kbFrame toView:self.controllerView];
    self.toolBarTextField.text = self.text;
    [self.toolBar addSubview:self.returnBtn];
    [self.toolBar addSubview:self.toolBarTextField];
    [self.controllerView addSubview:self.toolBar];

    CGRect textFiledInControllerFrame = [self.superview convertRect:self.frame toView:self.controllerView];
    if (textFiledInControllerFrame.origin.y <= newKbFrame.origin.y - toolbarHeight) {
        return;
    }
    CGFloat toolBarY = newKbFrame.origin.y - toolbarHeight;
    [UIView animateWithDuration:[[info valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue] delay:0 options:[[info valueForKey:UIKeyboardAnimationCurveUserInfoKey] intValue] animations:^{
        self.toolBar.frame = CGRectMake(0, toolBarY, kbFrame.size.width, toolbarHeight);
            } completion:^(BOOL finished){
                [self.toolBarTextField becomeFirstResponder];
    }];
}

- (void)keyboardWillHide:(NSNotification *)notification {
    NSDictionary *info = [notification userInfo];
    [UIView animateWithDuration:[[info valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue] delay:0 options:[[info valueForKey:UIKeyboardAnimationCurveUserInfoKey] intValue] animations:^{
        self.toolBar.frame = CGRectMake(0, self.controllerView.frame.size.height, self.controllerView.frame.size.width, toolbarHeight);
    } completion:^(BOOL finished){
    }];
}

- (void)returnBtnClick:(UIButton *)button{
    [self.toolBarTextField resignFirstResponder];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (UIViewController*)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        //如果你的controller继承了另一个controller  那么请修改这里的class类型
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;  
}

- (void)textFieldChanged{
    self.text = self.toolBarTextField.text;
}

@end

项目地址

github下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值