IOS 键盘弹出遮挡输入框使view上移
2016-01-07 22:56
417 查看
有时候输入框在屏幕下方,键盘弹出遮挡了输入框有种盲输入的赶脚,那我们就可以把视图向上移动让它露出来。
首先在viewDidLoad中注册键盘显隐通知
//注册键盘弹出通知
[[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 *)note
{
NSDictionary *info = [note
userInfo];
CGSize keyboardSize = [[info
objectForKey:UIKeyboardFrameEndUserInfoKey]
CGRectValue].size;
//目标视图UITextField
CGRect frame = passwordTF.frame;
int y = frame.origin.y + frame.size.height - (self.view.frame.size.height
- keyboardSize.height);
NSTimeInterval animationDuration =
0.30f;
[UIView
beginAnimations:@"ResizeView"
context:nil];
[UIView
setAnimationDuration:animationDuration];
if(y > 0)
{
self.view.frame =
CGRectMake(0, -y,
self.view.frame.size.width,
self.view.frame.size.height);
}
[UIView
commitAnimations];
}
//键盘隐藏后将视图恢复到原始状态
-(void)keyboardWillHide:(NSNotification *)note
{
NSTimeInterval animationDuration =
0.30f;
[UIView
beginAnimations:@"ResizeView"
context:nil];
[UIView
setAnimationDuration:animationDuration];
self.view.frame =CGRectMake(0,
0, self.view.frame.size.width,
self.view.frame.size.height);
[UIView
commitAnimations];
}
首先在viewDidLoad中注册键盘显隐通知
//注册键盘弹出通知
[[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 *)note
{
NSDictionary *info = [note
userInfo];
CGSize keyboardSize = [[info
objectForKey:UIKeyboardFrameEndUserInfoKey]
CGRectValue].size;
//目标视图UITextField
CGRect frame = passwordTF.frame;
int y = frame.origin.y + frame.size.height - (self.view.frame.size.height
- keyboardSize.height);
NSTimeInterval animationDuration =
0.30f;
[UIView
beginAnimations:@"ResizeView"
context:nil];
[UIView
setAnimationDuration:animationDuration];
if(y > 0)
{
self.view.frame =
CGRectMake(0, -y,
self.view.frame.size.width,
self.view.frame.size.height);
}
[UIView
commitAnimations];
}
//键盘隐藏后将视图恢复到原始状态
-(void)keyboardWillHide:(NSNotification *)note
{
NSTimeInterval animationDuration =
0.30f;
[UIView
beginAnimations:@"ResizeView"
context:nil];
[UIView
setAnimationDuration:animationDuration];
self.view.frame =CGRectMake(0,
0, self.view.frame.size.width,
self.view.frame.size.height);
[UIView
commitAnimations];
}
相关文章推荐
- iOS开发之小知识
- IOS下HTML5获取焦点 弹键盘
- js与Android、IOS数据交互
- iOS9上的Universal Link实现(教程)
- 【iOS】NSLog 打印 BOOL 类型值
- iOS WebView 如何通过js获取网页中所有图片并加入点击事件,实现浏览图片的功能
- ios项目中安装和使用CocoaPods
- 蓝懿ios技术交流和心得分享16.1.7
- IOS 开发流程
- iOS SDK:预览和打开文档
- iOS开发笔记9:NSUserDefaults存储自定义实体对象
- iOS小数点格式化:如果有两位小数不为0则保留两位小数,如果有一位小数不为0则保留一位小数,否则显示整数
- iOS_CoreData的使用
- iOS编程 -- webView的使用
- iOS 添加 framework 报缺少头文件
- 一只入了IOS深坑的汪
- iOS企业级开发
- RedHat 6.5(x86_64)启动nagios客户端nrpe报错的解决方法
- IOS-0811按钮操作(笔记)
- iOS -- 界面跳转--常遇到的bug