前言:在我們ios開發(fā)過程中經(jīng)常會遇到鍵盤遮擋問題河咽,解決方法钠右,網(wǎng)上搜索一大片,但總結(jié)起來其實就以下兩點:
一库北、監(jiān)聽鍵盤的響應事件
? ? (1)監(jiān)聽鍵盤的響應事件上升:UIKeyboardWillShowNotification 爬舰;鍵盤隱藏UIKeyboardWillHideNotification
? ? ?(2)在UIKeyboardWillShowNotification事件中確定TextField、TextView在屏幕中的位置寒瓦,然后再移動位置
? ? ? (3)在UIKeyboardWillHideNotification事件中復原位置
二情屹、在TextField、TextView的代理中textFieldDidBeginEditing 確定TextField杂腰、TextView在屏幕中位置垃你,然后在移動位置。
然而不管你使用上面的那種方法都會破壞你原本代碼結(jié)構(gòu)的完整性喂很,所以本人就將其封裝在一個object類中惜颇,這樣不僅重用性高,代碼結(jié)構(gòu)完整少辣,使用更是方便凌摄,可以說是三句代碼就解決遮擋問題。
使用時只需要將WindyKeyBoard.m和WindyKeyBoard.h文件拖進工程
@property(nonatomic,strong)WindyKeyBoard*keyBoard;并實現(xiàn)其代理WindyKeyBoardDelegate就可以了漓帅,_keyBoard為全局變量
然后在
- (void)viewDidLoad {
}
方法中加入如下代碼:
_keyBoard= [[WindyKeyBoardalloc]init];
_keyBoard.delegate=self;
完整項目git包含了各種情況下的TextField锨亏、TextView鍵盤遮擋問題,有需要的可以去下載忙干,git地址為:https://github.com/cocoaliaolei/TextField_TextView.git 覺得有用的客官幫忙給個star吧器予,謝謝了
上代碼:創(chuàng)建一個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;
}];
}
/*
添加鍵盤監(jiān)聽
*/
-(void)addNotificationKeyBoard{
[[NSNotificationCenterdefaultCenter]addObserver:self
selector:@selector(keyBoardShow:)
name:UIKeyboardWillShowNotificationobject:nil];
[[NSNotificationCenterdefaultCenter]addObserver:self
selector:@selector(keyBoardHidden:)
name:UIKeyboardWillHideNotificationobject:nil];
}
/**
*創(chuàng)建取消鍵盤的按鈕
*/
-(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