标签:
下载链接:https://github.com/cdpenggod/CDPMonitorKeyboard
CDPMonitorKeyboard.h
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface CDPMonitorKeyboard : NSObject //获取其单例 +(CDPMonitorKeyboard *)defaultMonitorKeyboard; //键盘出现时调用方法 -(void)keyboardWillShowWithSuperView:(UIView *)superView andNotification:(NSNotification *)notification higherThanKeyboard:(NSInteger)valueOfTheHigher; //键盘消失时调用方法 -(void)keyboardWillHide; @end
CDPMonitorKeyboard.m
#import "CDPMonitorKeyboard.h" @implementation CDPMonitorKeyboard{ UIView *_superView;//输入view的父view CGRect _superViewOldFrame; } //单例化 +(CDPMonitorKeyboard *)defaultMonitorKeyboard{ static CDPMonitorKeyboard *monitorKeyboard= nil; @synchronized(self){ if (!monitorKeyboard) { monitorKeyboard=[[self alloc] init]; } } return monitorKeyboard; } //当键盘出现时调用方法 -(void)keyboardWillShowWithSuperView:(UIView *)superView andNotification:(NSNotification *)notification higherThanKeyboard:(NSInteger)valueOfTheHigher{ _superView=superView; _superViewOldFrame=superView.frame; //获取键盘的高度 NSDictionary *userInfo = [notification userInfo]; NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; CGRect keyboardRect = [aValue CGRectValue]; NSInteger height = keyboardRect.size.height; for (UIView *view in superView.subviews) { if (view.isFirstResponder==YES) { NSInteger value=superView.bounds.size.height-view.frame.origin.y-view.bounds.size.height; if (value<height) { [UIView animateWithDuration:0.3 animations:^{ //防止超出视图最大范围 if (value-height-valueOfTheHigher+height<=0) { superView.frame=CGRectMake(0,-height,superView.bounds.size.width,superView.bounds.size.height); } else{ superView.frame=CGRectMake(0,value-height-valueOfTheHigher,superView.bounds.size.width,superView.bounds.size.height); } }]; } } } //tableView判断 if (superView.class==[UITableView class]) { UITableView *tableView=(UITableView *)superView; for (UITableViewCell *cell in [[superView.subviews objectAtIndex:0] subviews]) { for (UIView *view in [[cell.subviews objectAtIndex:0] subviews]) { if (view.isFirstResponder==YES) { UIView *cellView=view.superview.superview; float value1=cellView.frame.origin.y+cellView.bounds.size.height-tableView.contentOffset.y; float value2=tableView.bounds.size.height-value1; float value=value2+(superView.superview.bounds.size.height-superView.bounds.size.height-superView.frame.origin.y); if (value<height) { [UIView animateWithDuration:0.3 animations:^{ //防止超出视图最大范围 if (value-height-valueOfTheHigher+height<=0) { superView.frame=CGRectMake(0,_superViewOldFrame.origin.y-height,superView.bounds.size.width,superView.bounds.size.height); } else{ superView.frame=CGRectMake(0,_superViewOldFrame.origin.y+(value-height-valueOfTheHigher),superView.bounds.size.width,superView.bounds.size.height); } }]; } } } } } } //当键退出时调用 -(void)keyboardWillHide{ [UIView animateWithDuration:0.3 animations:^{ if (_superView) { //_superView.frame=_superViewOldFrame;
//注意,这里修改了源代码,避免输入中文出现bug _superView.frame=[UIScreen mainScreen].bounds; } }]; _superView=nil; } @end
使用方法:
1.创建通知
//增加监听,当键盘出现或改变时收出消息 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; //增加监听,当键退出时收出消息 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
2.当键盘打开关闭的时候调用CDPMonitorKeyboard的方法
#pragma mark 键盘监听方法设置 //当键盘出现时调用 -(void)keyboardWillShow:(NSNotification *)aNotification{ //第一个参数写输入view的父view即可,第二个写监听获得的notification,第三个写希望高于键盘的高度(只在被键盘遮挡时才启用,如控件未被遮挡,则无变化) //如果想不通输入view获得不同高度,可自己在此方法里分别判断区别 [[CDPMonitorKeyboard defaultMonitorKeyboard] keyboardWillShowWithSuperView:self.view andNotification:aNotification higherThanKeyboard:0]; } //当键退出时调用 -(void)keyboardWillHide:(NSNotification *)aNotification{ [[CDPMonitorKeyboard defaultMonitorKeyboard] keyboardWillHide]; }
3.移除通知
//dealloc中需要移除监听 -(void)dealloc{ //移除监听,当键盘出现或改变时收出消息 [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; //移除监听,当键退出时收出消息 [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; }
标签:
原文地址:http://www.cnblogs.com/hxwj/p/4534816.html