对UITextField的键盘处理方法
2011-11-08 14:52
190 查看
- (void)registerForKeyboardNotifications
{
//添加自己做为观察者,以获取键盘显示时的通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
//添加自己做为观察者,以获取键盘隐藏时的通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasHidden:)
name:UIKeyboardDidHideNotification object:nil];
}
// 键盘出现时调用此方法
- (void)keyboardWasShown:(NSNotification*)aNotification
{
//如果键盘是显示状态,不用做重复的操作
if (keyboardShown)
return;
//获得键盘通知的用户信息字典
NSDictionary* info = [aNotification userInfo];
// 取得键盘尺寸.
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// 重新设置scrollView的size
CGRect viewFrame = [myScrollView frame];
viewFrame.size.height -= keyboardSize.height;
myScrollView.frame = viewFrame;
// 把当前被挡住的text field滚动到view中适当的可见位置.
CGRect textFieldRect = [activeField frame];
[myScrollView scrollRectToVisible:textFieldRect animated:YES];
//记录当前textField的偏移量,方便隐藏键盘时,恢复textField到原来位置
oldContentOffsetValue = [myScrollView contentOffset].y;
//计算textField滚动到的适当位置
CGFloat value = (activeField.frame.origin.y+myScrollView.frame.origin.y+activeField.frame.size.height - self.view.frame.size.height + keyboardSize.height)+2.0f;
//value>0则表示需要滚动,小于0表示当前textField没有被挡住,不需要滚动
if (value > 0) {
//使textField滚动到适当位置
[myScrollView setContentOffset:CGPointMake(0, value) animated:YES];
isNeedSetOffset = YES;//更改状态标志为需要滚动
}
//更改键盘状态标志为已显示
keyboardShown = YES;
}
// 键盘隐藏时调用此方法
- (void)keyboardWasHidden:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
// Get the size of the keyboard.
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Reset the height of the scroll view to its original value
CGRect viewFrame = [myScrollView frame];
viewFrame.size.height += keyboardSize.height;
myScrollView.frame = viewFrame;
//如果状态标志为需要滚动,则要执行textFiled复位操作
if (isNeedSetOffset) {
//oldContentOffsetValue记录了textField原来的位置,复位即可
[myScrollView setContentOffset:CGPointMake(0, oldContentOffsetValue) animated:YES];
}
//复位状态标志
isNeedSetOffset = NO;
keyboardShown = NO;
}
{
//添加自己做为观察者,以获取键盘显示时的通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
//添加自己做为观察者,以获取键盘隐藏时的通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasHidden:)
name:UIKeyboardDidHideNotification object:nil];
}
// 键盘出现时调用此方法
- (void)keyboardWasShown:(NSNotification*)aNotification
{
//如果键盘是显示状态,不用做重复的操作
if (keyboardShown)
return;
//获得键盘通知的用户信息字典
NSDictionary* info = [aNotification userInfo];
// 取得键盘尺寸.
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// 重新设置scrollView的size
CGRect viewFrame = [myScrollView frame];
viewFrame.size.height -= keyboardSize.height;
myScrollView.frame = viewFrame;
// 把当前被挡住的text field滚动到view中适当的可见位置.
CGRect textFieldRect = [activeField frame];
[myScrollView scrollRectToVisible:textFieldRect animated:YES];
//记录当前textField的偏移量,方便隐藏键盘时,恢复textField到原来位置
oldContentOffsetValue = [myScrollView contentOffset].y;
//计算textField滚动到的适当位置
CGFloat value = (activeField.frame.origin.y+myScrollView.frame.origin.y+activeField.frame.size.height - self.view.frame.size.height + keyboardSize.height)+2.0f;
//value>0则表示需要滚动,小于0表示当前textField没有被挡住,不需要滚动
if (value > 0) {
//使textField滚动到适当位置
[myScrollView setContentOffset:CGPointMake(0, value) animated:YES];
isNeedSetOffset = YES;//更改状态标志为需要滚动
}
//更改键盘状态标志为已显示
keyboardShown = YES;
}
// 键盘隐藏时调用此方法
- (void)keyboardWasHidden:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
// Get the size of the keyboard.
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Reset the height of the scroll view to its original value
CGRect viewFrame = [myScrollView frame];
viewFrame.size.height += keyboardSize.height;
myScrollView.frame = viewFrame;
//如果状态标志为需要滚动,则要执行textFiled复位操作
if (isNeedSetOffset) {
//oldContentOffsetValue记录了textField原来的位置,复位即可
[myScrollView setContentOffset:CGPointMake(0, oldContentOffsetValue) animated:YES];
}
//复位状态标志
isNeedSetOffset = NO;
keyboardShown = NO;
}
相关文章推荐
- iOS 控件之UITextField及键盘挡住处理方法(欢迎大家提建议和分享)
- <转>对UITextField的键盘处理方法
- 键盘遮挡的处理。UITextField在实际的应用(综合应用, 有点小难度)
- iOS中多个UITextField的键盘处理
- iOS 上常用的两个功能:点击屏幕和return退出隐藏键盘和解决虚拟键盘挡住UITextField的方法
- .Net 2.0 DataGridView中键盘事件处理方法
- iOS开发之UITextField输入长度限制,键盘遮挡自适应方法
- 笔记本键盘串键处理方法
- 《React-Native系列》42、键盘遮挡问题官方处理方法KeyboardAvoidingView
- iOS-键盘常见处理,自定义键盘上的工具条、定义键自盘方法等
- 处理键盘遮挡视图的方法
- 【转】iOS 上常用的两个功能:点击屏幕和return退出隐藏键盘和解决虚拟键盘挡住UITextField的方法
- QT 键盘 很多按键的处理方法
- ActiveX控件在浏览器中的鼠标键盘事件处理方法
- iOS --UITextField 输入值改变事件和键盘遮挡处理
- iOS UITextField禁止键盘弹出,同时又实现代理方法的实现方式!
- iPhone开发 当输入的键盘会挡住UITextField的处理事件
- 键盘弹出,视图向上移,键盘高度遮住UITextField的解决方法
- 如何处理键盘遮挡UITextField
- [引].Net 2.0 DataGridView中键盘事件处理方法