前言:在我们ios开发过程中经常会遇到键盘遮挡问题,解决方法,网上搜索一大片,但总结起来其实就以下两点:
一、监听键盘的响应事件
(1)监听键盘的响应事件上升:UIKeyboardWillShowNotification ;键盘隐藏UIKeyboardWillHideNotification
(2)在UIKeyboardWillShowNotification事件中确定TextField、TextView在屏幕中的位置,然后再移动位置
(3)在UIKeyboardWillHideNotification事件中复原位置
二、在TextField、TextView的代理中textFieldDidBeginEditing 确定TextField、TextView在屏幕中位置,然后在移动位置。
然而不管你使用上面的那种方法都会破坏你原本代码结构的完整性,所以本人就将其封装在一个object类中,这样不仅重用性高,代码结构完整,使用更是方便,可以说是三句代码就解决遮挡问题。
使用时只需要将WindyKeyBoard.m和WindyKeyBoard.h文件拖进工程
@property(nonatomic,strong)WindyKeyBoard*keyBoard;并实现其代理WindyKeyBoardDelegate就可以了,_keyBoard为全局变量
然后在
- (void)viewDidLoad {
}
方法中加入如下代码:
_keyBoard= [[WindyKeyBoardalloc]init];
_keyBoard.delegate=self;
完整项目git包含了各种情况下的TextField、TextView键盘遮挡问题,有需要的可以去下载,git地址为:https://github.com/cocoaliaolei/TextField_TextView.git 觉得有用的客官帮忙给个star吧,谢谢了
上代码:创建一个NSObject 类
.h文件如下:
//
//WindyKeyBoard.h
//WindKeyboard
//
//Created by winds on 2017/8/22.
//Copyright © 2017年东边的风. All rights reserved.
#import
@classWindyKeyBoard;
@protocolWindyKeyBoardDelegate
@optional
-(void)WindyKeyBoard:(BOOL)isUp withNotification:(NSNotification*)notifi;
-(void)WindyKeyBoardBtnClick:(UIButton*)btn;
@end
@interfaceWindyKeyBoard :NSObject
/**
*是否开启自动避免键盘遮挡
*默认YES
*/
@property(nonatomic,assign)BOOLisSuitable;
/**
*是否显示键盘上隐藏按钮
*默认YES
*/
@property(nonatomic,assign)BOOLisShowDownBtn;
/**
* delegate:只能为UIViewColtroller或UIView 或其子类
*/
@property(nonatomic,weak)iddelegate;
@end
.m文件如下:
//
//WindyKeyBoard.m
//WindKeyboard
//
//Created by winds on 2017/8/22.
//Copyright © 2017年东边的风. All rights reserved.
//
#import"WindyKeyBoard.h"
@interfaceWindyKeyBoard()
{
__blockCGFloatoriginalY;
}
@property(nonatomic,strong)UIButton*btn;
@end
@implementationWindyKeyBoard
/**
*btnWide隐藏键盘按钮宽度
*btnHeight隐藏键盘按钮宽度
*/
staticCGFloatbtnWide=40;
staticCGFloatbtnHeight =30;
-(void)dealloc{
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillShowNotificationobject:nil];
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillHideNotificationobject:nil];
[_btnremoveFromSuperview];
_btn=nil;
NSLog(@"--------%s",__FUNCTION__);
}
-(UIButton*)btn{
if(!_btn) {
_btn= [UIButtonbuttonWithType:UIButtonTypeCustom];
_btn.frame=CGRectMake(WD-btnWide,HG,btnWide,btnHeight);
[_btnaddTarget:selfaction:@selector(downKeyBoard:)forControlEvents:UIControlEventTouchUpInside];
[_btnsetImage:[UIImageimageNamed:@"dropdownmnue"]forState:UIControlStateNormal];
_btn.layer.cornerRadius=3;
_btn.layer.masksToBounds=YES;
_btn.backgroundColor= [UIColorredColor];
}
return_btn;
}
- (instancetype)init
{
self= [superinit];
if(self) {
[selfinitSetUp];
[selfaddNotificationKeyBoard];
[selfcreatBtn];
}
returnself;
}
-(void)setDelegate:(id)delegate{
_delegate= delegate;
/*
获取view的原始位置
*/
[selfviewAction:^(UIView*view) {
originalY= view.y;
}];
}
/*
添加键盘监听
*/
-(void)addNotificationKeyBoard{
[[NSNotificationCenterdefaultCenter]addObserver:self
selector:@selector(keyBoardShow:)
name:UIKeyboardWillShowNotificationobject:nil];
[[NSNotificationCenterdefaultCenter]addObserver:self
selector:@selector(keyBoardHidden:)
name:UIKeyboardWillHideNotificationobject:nil];
}
/**
*创建取消键盘的按钮
*/
-(void)creatBtn{
[[UIApplicationsharedApplication].keyWindowaddSubview:self.btn];
}
-(void)initSetUp{
self.isSuitable=YES;
self.isShowDownBtn=YES;
}
-(void)setIsShowDownBtn:(BOOL)isShowDownBtn{
_isShowDownBtn= isShowDownBtn;
self.btn.hidden= !isShowDownBtn;
}
-(void)downKeyBoard:(UIButton*)button{
[selfviewAction:^(UIView*view) {
[viewendEditing:YES];
}];
if(_delegate&& [_delegaterespondsToSelector:@selector(WindyKeyBoardBtnClick:)]) {
[_delegateWindyKeyBoardBtnClick:button];
}
}
/**
*键盘升起
*/
-(void)keyBoardShow:(NSNotification*)notifi{
floatheight = [[notifi.userInfoobjectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size.height;
_btn.x=WD-_btn.wide;
[UIViewanimateWithDuration:0.23animations:^{_btn.y=HG- height -_btn.height;}];
[selfsetTextFieldLocation:heightupOrDown:YES];
if(_delegate&& [_delegaterespondsToSelector:@selector(WindyKeyBoard:withNotification:)]) {
[_delegateWindyKeyBoard:YESwithNotification:notifi];
}
}
-(void)setTextFieldLocation:(CGFloat)kHeight upOrDown:(BOOL)isUp{
if(_isSuitable) {
[selfviewAction:^(UIView*view) {
UITextField*textField = [selffindFirstResponder:view];
if(isUp) {//键盘升起- view向上偏移
CGRectrect = [textFieldconvertRect:textField.boundstoView:[UIApplicationsharedApplication].keyWindow];
CGFloatlocationY =CGRectGetMaxY(rect);
if( locationY >HG- kHeight) {
[UIViewanimateWithDuration:0.23animations:^{
view.y= view.y- (rect.origin.y+ rect.size.height+ kHeight -HG);
}];
}
}
else{//键盘隐藏-还原到初始位置
[UIViewanimateWithDuration:0.23animations:^{
view.y=originalY;
}];
}
}];
}
}
-(void)keyBoardHidden:(NSNotification*)notifi{
floatheight = [[notifi.userInfoobjectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size.height;
[UIViewanimateWithDuration:0.23animations:^{
_btn.y=HG;
}];
[selfsetTextFieldLocation:heightupOrDown:NO];
if(_delegate&& [_delegaterespondsToSelector:@selector(WindyKeyBoard:withNotification:)]) {
[_delegateWindyKeyBoard:NOwithNotification:notifi];
}
}
/**
*返回view
*使用block操作不同情况
*/
-(void)viewAction:(void(^)(UIView*))block{
if([_delegateisKindOfClass:[UIViewControllerclass]]) {
UIViewController*ctl = (UIViewController*)_delegate;
if(block) block(ctl.view);
}
elseif([_delegateisKindOfClass:[UIViewclass]]){
UIView*view = (UIView*)_delegate;
if(block) block(view);
}
}
/*
找出当前响应的TextField
*/
-(UITextField*)findFirstResponder:(UIView*)view
{
if(view.isFirstResponder) {
return(UITextField*)view;
}
for(UIView*subViewinview.subviews) {
UIView*firstResponder = [selffindFirstResponder:subView];
if(firstResponder !=nil) {
return(UITextField*)firstResponder;
}
}
returnnil;
}
@end
网友评论