在开发IOS项目时计算键盘的高度
2015-06-02 17:26
537 查看
在IOS项目开发过程中,有时候需要计算键盘的高度,通过键盘的高度去动态的显示一些需要用到键盘的控件。比如在输入密码和用户名时,为了不让弹出的键盘遮挡密码和用户名输入框,需要计算键盘的高度去改变输入框控件的位置。
以下是实现代码:
//可以在viewDidLoad方法中,也可以在需要弹出键盘的代码中,视具体情况而定!
- (void)viewDidLoad
{
[super viewDidLoad];
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
//当键盘出现或改变时调用
- (void)keyboardDidShow:(NSNotification *)aNotification
{
//获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
int height = keyboardRect.size.height;
}
//当键退出时调用
- (void)keyboardDidHide:(NSNotification *)aNotification
{
NSLog(@"-----键盘隐藏!------");
}
以下是实现代码:
//可以在viewDidLoad方法中,也可以在需要弹出键盘的代码中,视具体情况而定!
- (void)viewDidLoad
{
[super viewDidLoad];
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
//当键盘出现或改变时调用
- (void)keyboardDidShow:(NSNotification *)aNotification
{
//获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
int height = keyboardRect.size.height;
}
//当键退出时调用
- (void)keyboardDidHide:(NSNotification *)aNotification
{
NSLog(@"-----键盘隐藏!------");
}
相关文章推荐
- iOS plist 文件介绍
- ios pop 折叠动画
- iOS开发-控制状态栏
- IOS 文件读写常用的2中方法
- iOS开发零基础教程之Git的一些常用命令
- IOS申请发布证书-图文详解
- 从零开始系列之iOS地图获取当前城市
- iOS中如何通过instruments工具来找崩溃信息
- iOS 调试
- 最新版SDWebImage的使用
- ios代码行数
- 网易新闻iOS版使用的18个开源组件
- 关于ios
- iOS开发之版本控制(SVN)
- IOS 播放动态Gif图片
- IOS 播放动态Gif图片
- iOS开发之让你的应用“动”起来
- iOS 版本更新
- iOS程序在后台进行任务的方法
- 苹果要把Google Now装进iOS 9?