ios 软键盘的相关处理
2016-01-27 15:34
423 查看
1.触摸键盘以外的屏幕回收键盘
//回收键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
2.点击软键盘上完成按钮回收键盘
*在.m中签订协议<UITextFieldDelegate>
*在viewDidLoad中成为代理人
self.messageTextField.delegate = self;
*实现协议的方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField {//按“完成”隐藏键盘
[textField resignFirstResponder];//隐藏输入键盘
return YES;
}
3.动态获取键盘高度
*在工程的 -(void)viewDidload;函数中添加键盘弹出和隐藏的通知,具体代码如下:
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
*当得到通知时写2个函数,来响应通知 -(void)keyboardWillShow; -(void)keyboardWillHide;在这2个函数中可以得到键盘的一些属性,具体代码如下:
- (void)keyboardWillShow:(NSNotification *)aNotification
{
//获取键盘的高度
/*
iphone 6:
中文
2014-12-31 11:16:23.643 Demo[686:41289] 键盘高度是 258
2014-12-31 11:16:23.644 Demo[686:41289] 键盘宽度是 375
英文
2014-12-31 11:55:21.417 Demo[1102:58972] 键盘高度是 216
2014-12-31 11:55:21.417 Demo[1102:58972] 键盘宽度是 375
iphone 6 plus:
英文:
2014-12-31 11:31:14.669 Demo[928:50593] 键盘高度是 226
2014-12-31 11:31:14.669 Demo[928:50593] 键盘宽度是 414
中文:
2015-01-07 09:22:49.438 Demo[622:14908] 键盘高度是 271
2015-01-07 09:22:49.439 Demo[622:14908] 键盘宽度是 414
iphone 5 :
2014-12-31 11:19:36.452 Demo[755:43233] 键盘高度是 216
2014-12-31 11:19:36.452 Demo[755:43233] 键盘宽度是 320
ipad Air:
2014-12-31 11:28:32.178 Demo[851:48085] 键盘高度是 264
2014-12-31 11:28:32.178 Demo[851:48085] 键盘宽度是 768
ipad2 :
2014-12-31 11:33:57.258 Demo[1014:53043] 键盘高度是 264
2014-12-31 11:33:57.258 Demo[1014:53043] 键盘宽度是 768
*/
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
int height = keyboardRect.size.height;
int width = keyboardRect.size.width;
NSLog(@"键盘高度是 %d",height);
NSLog(@"键盘宽度是 %d",width);
}
//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
}
4.UITxetFiled自动适应软键盘高度
*宏定义视图的宽高
#define UISCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define UISCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
*将动态获取到的软键盘高度设置为属性
@property (nonatomic)int keyboardheight;
*根据软键盘的高度改变视图的高度
-(void)textFieldDidBeginEditing:(UITextField *)textField{
int offset = self.keyboardheight;
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
//将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示
if(offset > 0)
self.view.frame = CGRectMake(0.0f, -offset, UISCREEN_WIDTH, UISCREEN_HEIGHT);
[UIView commitAnimations];
}
//输入框编辑完成以后,将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField{
self.view.frame = CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT);
}
//回收键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
2.点击软键盘上完成按钮回收键盘
*在.m中签订协议<UITextFieldDelegate>
*在viewDidLoad中成为代理人
self.messageTextField.delegate = self;
*实现协议的方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField {//按“完成”隐藏键盘
[textField resignFirstResponder];//隐藏输入键盘
return YES;
}
3.动态获取键盘高度
*在工程的 -(void)viewDidload;函数中添加键盘弹出和隐藏的通知,具体代码如下:
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
*当得到通知时写2个函数,来响应通知 -(void)keyboardWillShow; -(void)keyboardWillHide;在这2个函数中可以得到键盘的一些属性,具体代码如下:
- (void)keyboardWillShow:(NSNotification *)aNotification
{
//获取键盘的高度
/*
iphone 6:
中文
2014-12-31 11:16:23.643 Demo[686:41289] 键盘高度是 258
2014-12-31 11:16:23.644 Demo[686:41289] 键盘宽度是 375
英文
2014-12-31 11:55:21.417 Demo[1102:58972] 键盘高度是 216
2014-12-31 11:55:21.417 Demo[1102:58972] 键盘宽度是 375
iphone 6 plus:
英文:
2014-12-31 11:31:14.669 Demo[928:50593] 键盘高度是 226
2014-12-31 11:31:14.669 Demo[928:50593] 键盘宽度是 414
中文:
2015-01-07 09:22:49.438 Demo[622:14908] 键盘高度是 271
2015-01-07 09:22:49.439 Demo[622:14908] 键盘宽度是 414
iphone 5 :
2014-12-31 11:19:36.452 Demo[755:43233] 键盘高度是 216
2014-12-31 11:19:36.452 Demo[755:43233] 键盘宽度是 320
ipad Air:
2014-12-31 11:28:32.178 Demo[851:48085] 键盘高度是 264
2014-12-31 11:28:32.178 Demo[851:48085] 键盘宽度是 768
ipad2 :
2014-12-31 11:33:57.258 Demo[1014:53043] 键盘高度是 264
2014-12-31 11:33:57.258 Demo[1014:53043] 键盘宽度是 768
*/
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
int height = keyboardRect.size.height;
int width = keyboardRect.size.width;
NSLog(@"键盘高度是 %d",height);
NSLog(@"键盘宽度是 %d",width);
}
//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
}
4.UITxetFiled自动适应软键盘高度
*宏定义视图的宽高
#define UISCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define UISCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
*将动态获取到的软键盘高度设置为属性
@property (nonatomic)int keyboardheight;
*根据软键盘的高度改变视图的高度
-(void)textFieldDidBeginEditing:(UITextField *)textField{
int offset = self.keyboardheight;
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
//将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示
if(offset > 0)
self.view.frame = CGRectMake(0.0f, -offset, UISCREEN_WIDTH, UISCREEN_HEIGHT);
[UIView commitAnimations];
}
//输入框编辑完成以后,将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField{
self.view.frame = CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT);
}
相关文章推荐
- ios记录日志和崩溃日志,并将日志文件发送到指定的邮箱
- iOS MD5加密
- iOS 使用Block实现函数回调
- iOS应用内语言切换功能
- iOS:iOS为什么要用-all_load、-ObjC、-force_load
- iOS6、7、8、9新特性汇总和适配说明
- iOS9 3D Touch 标签菜单 peek and pop force 压力触控 功能开发高级版教程
- iOS中@synthesize的使用
- iOS检测设备摄像头、指南针、录音、陀螺仪状态
- 【IOS】总结-AFNetworking学习笔记
- 集成支付宝钱包支付iOS SDK的方法与经验
- IOS自定义仪表盘
- iOS开发之应用内检测手机锁屏,解锁状态
- GIT使用
- iOS旋钮动画-CircleKnob
- iOS 官方文档 NSCalendar
- iOS使用Charles(青花瓷)抓包并篡改返回数据图文详解(转)
- iOS 9 添加白名单
- iOS程序猿之keyChain保存登录名和密码
- iOS数组元素随机排序