当键盘挡住输入框时候,可以使的界面自动上移
2016-06-13 17:43
543 查看
第一步:注册通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
第二步:实现方法:
- (void)keyboardWillShow:(NSNotification *)notification
{
NSDictionary * info = [notification userInfo];
NSValue *avalue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [self.view convertRect:[avalue CGRectValue] fromView:nil];
double keyboardHeight=keyboardRect.size.height;//键盘的高度
CGFloat offset = self.view.frame.size.height - ([addressTextFile superview].frame.origin.y + remarksTextView.frame.size.height + keyboardHeight + 50 * heightScale);
if (offset <= 0) {
[UIView animateWithDuration:0.3 animations:^{
CGRect frame = self.view.frame;
frame.origin.y = offset;
self.view.frame = frame;
}];
}
}
- (void)keyboardHide:(UITapGestureRecognizer*)tap{
[nameTextFile resignFirstResponder];
[phoneTextFile resignFirstResponder];
[addressTextFile resignFirstResponder];
[remarksTextView resignFirstResponder];
}
//第三步:移除通知
-(void)viewDidDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
第二步:实现方法:
- (void)keyboardWillShow:(NSNotification *)notification
{
NSDictionary * info = [notification userInfo];
NSValue *avalue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [self.view convertRect:[avalue CGRectValue] fromView:nil];
double keyboardHeight=keyboardRect.size.height;//键盘的高度
CGFloat offset = self.view.frame.size.height - ([addressTextFile superview].frame.origin.y + remarksTextView.frame.size.height + keyboardHeight + 50 * heightScale);
if (offset <= 0) {
[UIView animateWithDuration:0.3 animations:^{
CGRect frame = self.view.frame;
frame.origin.y = offset;
self.view.frame = frame;
}];
}
}
- (void)keyboardHide:(UITapGestureRecognizer*)tap{
[nameTextFile resignFirstResponder];
[phoneTextFile resignFirstResponder];
[addressTextFile resignFirstResponder];
[remarksTextView resignFirstResponder];
}
//第三步:移除通知
-(void)viewDidDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}
相关文章推荐
- [转载]Activity中ConfigChanges属性的用法
- 在 Linux 下交换 Ctrl 与大写锁定键
- CS 1.5 键盘基本操作快捷键
- C#控制键盘按键的常用方法
- C#检查键盘大小写锁定状态的方法
- winform异型不规则界面设计的实现方法
- 在ASP.NET 2.0中操作数据之二十:定制数据修改界面
- 按键盘方向键翻页跳转的javascript代码(支持ie,firefox)
- C++处理键盘输入的方法
- 键盘 keycode的值 javascript时触发事件时很有用的要素
- JavaScript取得键盘按下方向键是哪个的方法
- js鼠标、键盘事件实例代码
- jQuery实时显示鼠标指针位置和键盘ASCII码
- jQuery 监控键盘一段时间没输入
- 如何让div span等元素能响应键盘事件操作指南
- 安卓输入框被虚拟键盘挡住的问题(微信开发)
- 键盘扫描码[比较完整]
- javascript 响应键盘特定按键(只响应数字键)
- javascript键盘上下键的操作(选择)
- 使用钩子如何锁定键盘的方法分享