您的位置:首页 > 移动开发 > IOS开发

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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: