iOS解决键盘遮挡的神器
2017-05-12 本文已影响63人
huangxiongbiao
注:代码并非原创,以前找帖子拿来用的忘记原贴了。只是留个笔记,非原创勿喷
- 主要思路通过UIResponder类别拦截,init方法注册键盘打开关闭的方法。然后实现相关的逻辑计算遮挡部分的高度,调整坐标解决遮挡
代码
#import <UIKit/UIKit.h>
@interface UIResponder (BGKeyboardMgr)
@end
#import "UIResponder+BGKeyboardMgr.h"
#import <objc/runtime.h>
static CGRect initFrame;
static UIButton *endButton=nil;
#define MainWindow [[UIApplication sharedApplication].delegate window]
#define IsEvent [self isKindOfClass:[UITextField class]]||[self isKindOfClass:[UITextView class]]
@implementation UIResponder (BGKeyboardMgr)
/**
实现UIResponder
**/
-(instancetype)init {
if (self=[super init]) {
[self registerKeyboardManagement];//注册键盘的监听
}
return self;
}
#pragma mark - ============ 其他 ============
/**
注册键盘的监听
**/
-(void)registerKeyboardManagement {
if (IsEvent) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardNotificationAction:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardNotificationAction:) name:UIKeyboardWillShowNotification object:nil];
}
}
-(void)dealloc {
if (IsEvent) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}
}
#pragma mark - ============ 给类触发事件 ============
#pragma mark - 键盘通知
- (void)keyboardNotificationAction:(NSNotification *)notification {
if (!self.isFirstResponder) {
} else {
if ([notification.name isEqualToString:UIKeyboardWillHideNotification]) {
UIView *view =[MainWindow.subviews objectAtIndex:0];
NSValue *value =[NSValue valueWithCGRect:initFrame];
if (!([value isEqualToValue:[NSValue valueWithCGRect:CGRectNull]]||[value isEqualToValue:[NSValue valueWithCGRect:CGRectZero]])) {
view.frame=initFrame;
initFrame=CGRectZero;
}
[endButton removeFromSuperview];
MainWindow.backgroundColor=[UIColor clearColor];
}
else if ([notification.name isEqualToString:UIKeyboardWillShowNotification]) {
CGRect keyboardFrame = ((NSValue *) notification.userInfo[UIKeyboardFrameEndUserInfoKey]).CGRectValue;
UIView *sfView=(UIView*)self;
/**
计算输入框相对于MainWindow的位置
**/
CGRect rect =[MainWindow convertRect:sfView.frame fromView:sfView.superview];
CGFloat tfMaxY = rect.origin.y+rect.size.height;
if (rect.origin.y<kScreenHeight-49) {
tfMaxY = tfMaxY>kScreenHeight?kScreenHeight-49-22:tfMaxY;
}else {
tfMaxY = tfMaxY>kScreenHeight?kScreenHeight:tfMaxY;
}
CGFloat y=tfMaxY-keyboardFrame.origin.y;
UIView *view =[MainWindow.subviews objectAtIndex:0];
NSValue *value =[NSValue valueWithCGRect:initFrame];
if ([value isEqualToValue:[NSValue valueWithCGRect:CGRectNull]]||[value isEqualToValue:[NSValue valueWithCGRect:CGRectZero]]) {
initFrame=view.frame;
}
if (!endButton) {
endButton=[UIButton buttonWithType:UIButtonTypeSystem];
endButton.frame=CGRectMake(keyboardFrame.size.width-69, keyboardFrame.origin.y-22, 64, 22);
endButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
[endButton setTitle:@"结束编辑" forState:UIControlStateNormal];
endButton.titleLabel.font=[UIFont systemFontOfSize:15.0f];
[endButton addTarget:MainWindow action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}
[MainWindow addSubview:endButton];
MainWindow.backgroundColor=[UIColor whiteColor];
if (y>0) {
view.y = view.y-y-endButton.height;
// view.frame=CGRectMake(view.frame.origin.x, view.frame.origin.y-y-endButton.frame.size.height, view.frame.size.width, view.frame.size.height);
}
}
}
}
#pragma mark - buttonAction
-(void)buttonAction:(UIButton *)sender {
[MainWindow endEditing:YES];
}
@end