iOS 键盘弹出时获取键盘的高度
2016-04-20 14:25
471 查看
1、在viewDidLoad方法中加入监测键盘的通知。
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view.
[[NSNotificationCenter
defaultCenter]
addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter
defaultCenter]
addObserver:self
selector:@selector(keyboardWillHidden:)
name:UIKeyboardWillHideNotification
object:nil];
}
2、实现通知的方法
/**
* 键盘将要显示
*
* @param notification
通知
*/
-(void)keyboardWillShow:(NSNotification *)notification
{
//这样就拿到了键盘的位置大小信息frame,然后根据frame进行高度处理之类的信息
CGRect frame = [[[notification
userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]
CGRectValue];
CGFloat endHeight =
self.showScrollView.contentSize.height + frame.size.height;
self.showScrollView.contentSize =
CGSizeMake(SCREEN_WIDTH, endHeight);
self.showScrollView.contentOffset =
CGPointMake(0,
self.bottomView.originY);
}
/**
* 键盘将要隐藏
*
* @param notification
通知
*/
-(void)keyboardWillHidden:(NSNotification *)notification
{
CGRect frame = [[[notification
userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]
CGRectValue];
CGFloat endHeight =
self.showScrollView.contentSize.height - frame.size.height;
self.showScrollView.contentSize =
CGSizeMake(SCREEN_WIDTH, endHeight);
}
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view.
[[NSNotificationCenter
defaultCenter]
addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter
defaultCenter]
addObserver:self
selector:@selector(keyboardWillHidden:)
name:UIKeyboardWillHideNotification
object:nil];
}
2、实现通知的方法
/**
* 键盘将要显示
*
* @param notification
通知
*/
-(void)keyboardWillShow:(NSNotification *)notification
{
//这样就拿到了键盘的位置大小信息frame,然后根据frame进行高度处理之类的信息
CGRect frame = [[[notification
userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]
CGRectValue];
CGFloat endHeight =
self.showScrollView.contentSize.height + frame.size.height;
self.showScrollView.contentSize =
CGSizeMake(SCREEN_WIDTH, endHeight);
self.showScrollView.contentOffset =
CGPointMake(0,
self.bottomView.originY);
}
/**
* 键盘将要隐藏
*
* @param notification
通知
*/
-(void)keyboardWillHidden:(NSNotification *)notification
{
CGRect frame = [[[notification
userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]
CGRectValue];
CGFloat endHeight =
self.showScrollView.contentSize.height - frame.size.height;
self.showScrollView.contentSize =
CGSizeMake(SCREEN_WIDTH, endHeight);
}
相关文章推荐
- ios开发多线程篇——GCD的基本使用
- iOS之在写一个iOS应用之前必须做的7件事(附相关资源)
- iOS获取屏幕亮度和调整屏幕亮度的方法
- iOS之17个提升iOS开发效率的必用工具
- iOS开发 ☞ Commen Sense
- ios开发多线程篇——线程的状态
- block的声明创建以及使用
- iOS OC readOnly 和属性 详解
- iOS 保证视频的清晰度和流畅度
- 关于iOS开发企业应用问题整理
- iOS 定位 <CoreLocation/CoreLocation.h>
- IOS环信3.1 -SDK集成
- iOS学习笔记38-MJExtension使用
- iOS 摇一摇功能的实现
- iOS学习笔记38-MJExtension使用
- iOS 手势
- IOS 二维码生成
- ios开发多线程篇——GCD介绍
- ios 指定某个页面是横屏还是竖屏
- iOS证书描述文件说明