IOS 键盘使用
来源:互联网 发布:好听好记的网站域名 编辑:程序博客网 时间:2024/04/30 06:58
在点击录入框时键盘会自动升起,这是系统自动操作,但很多时候键盘会把视图中控件挡住,这样我们就得对键盘升做一些操作,
首先,要监听键盘升起事件,系统在升起键盘时会发出UIKeyboardWillShowNotification信号,要捕捉它并设置处理函数。
[[NSNotificationCenterdefaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
keyboardWillShow是处理函数,在函数做视图位置上升变化。键盘处理函数中改变视图位置要注意个问题,如果键盘弹出不是英文键盘,会收到多次信号,这是因为系统在切换键盘时都会发出keyboardWillShow信号。比如现在使用的是中文输入法,系统首先弹出键盘是英文键盘会发出信号,然后在切换到中文键盘又会发出信号,
还要监听键盘降下事件,在键盘落下时把视图位置反回原处,键盘落下系统发出UIKeyboardWillHideNotification信号
[[NSNotificationCenterdefaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
键盘处理函数
-(void) keyboardWillShow:(NSNotification *)note{
//取得键盘位置高度
CGRect keyboardBounds;
[[note.userInfovalueForKey:UIKeyboardFrameEndUserInfoKey]getValue: &keyboardBounds];
NSNumber *duration = [note.userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [note.userInfoobjectForKey:UIKeyboardAnimationCurveUserInfoKey];
// Need to translate the bounds to account for rotation.
keyboardBounds = [self.viewconvertRect:keyboardBoundstoView:nil];
//改变视图位置
CGRect containerFrame =containerView.frame;
containerFrame.origin.y =self.view.bounds.size.height - keyboardBounds.size.height;
// animations settings
[UIViewbeginAnimations:nilcontext:NULL];
[UIViewsetAnimationBeginsFromCurrentState:YES];
[UIViewsetAnimationDuration:[durationdoubleValue]];
[UIViewsetAnimationCurve:[curveintValue]];
// set views with new info
containerView.frame = containerFrame;
// commit animations
[UIViewcommitAnimations];
}
-(void) keyboardWillHide:(NSNotification *)note{
NSNumber *duration = [note.userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [note.userInfoobjectForKey:UIKeyboardAnimationCurveUserInfoKey];
// get a rect for the textView frame
CGRect containerFrame =containerView.frame;
containerFrame.origin.y =self.view.bounds.size.height - containerFrame.size.height +20;
// animations settings
[UIViewbeginAnimations:nilcontext:NULL];
[UIViewsetAnimationBeginsFromCurrentState:YES];
[UIViewsetAnimationDuration:[durationdoubleValue]];
[UIViewsetAnimationCurve:[curveintValue]];
// set views with new info
containerView.frame = containerFrame;
// commit animations
[UIViewcommitAnimations];
}
也可以监听某些按件,让某个控件关闭键盘
-(IBAction)sendMessage_Click:(id)sender
{
[textViewresignFirstResponder];
}
- 关于IOS键盘使用
- IOS 键盘使用
- iOS键盘使用
- iOS 使用UITextField隐藏键盘
- iOS 使用UITextField隐藏键盘
- iOS 键盘框架IQKeyboardManager使用
- iOS UITextfield不弹出键盘 使用自定义键盘
- iOS 键盘风格和使用详解
- iOS 键盘自适应(IQKeyboardManager)使用小结
- iOS 键盘自适应(IQKeyboardManager)使用小结
- IOS开发中使用自定义键盘
- iOS 键盘自适应(IQKeyboardManager)使用小结
- iOS 键盘通知(NSNotificationCenter)正确使用
- iOS 键盘中使用[UIApplication sharedApplication].statusBarFrame
- iOS-关于 键盘 IQKeyboardManager的使用
- iOS 之IQKeyboardManager键盘的使用
- iOS键盘监控键盘
- IOS 键盘
- java的练习
- Android学习(2):Android系统
- Twisted 知识
- hdu2054A == B ?
- mysql-front导入导出数据乱码的问题
- IOS 键盘使用
- Android中设置颜色的几种方式
- impdp 报错ORA-31693
- IPC方案
- C/C++获取当前系统时间
- win7配置双网卡与centos相互连通
- 假前综合症
- android实现透明的颜色效果
- hdu 2212