iOS键盘弹出 视图向上滚动键盘高度
2016-11-02 09:01
381 查看
首先要对键盘添加监听:
在viewDidLoad中添加如下代码:
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillAppear:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillDisappear:)
name:UIKeyboardWillHideNotification object:nil];
当系统消息出现UIKeyboardWillShowNotification和UIKeyboardWillHideNotification消息就会调用我们的keyboardWillAppear和keyboardWillDisappear方法。
其次键盘的高度计算:
-(CGFloat)keyboardEndingFrameHeight:(NSDictionary *)userInfo//计算键盘的高度
{
CGRect keyboardEndingUncorrectedFrame
= [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
CGRect keyboardEndingFrame
= [self.view convertRect:keyboardEndingUncorrectedFrame fromView:nil];
return keyboardEndingFrame.size.height;
}
传入的(NSDictionary *)userInfo用于存放键盘的各种信息,其中UIKeyboardFrameEndUserInfoKey对应的存放键盘的尺寸信息,以CGRect形式取出。
// 将rect由rect所在视图转换到
effa
目标视图view中,返回在目标视图view中的rect
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
// 将rect从view中转换到当前视图中,返回在当前视图中的rect
- (CGRect)convertRect:(CGRect)rect
fromView:(UIView *)view;
最终返回的是键盘在当前视图中的高度。
然后,根据键盘高度将当前视图向上滚动同样高度。
-(void)keyboardWillAppear:(NSNotification *)notification
{
CGRect currentFrame = self.view.frame;
CGFloat change = [self keyboardEndingFrameHeight:[notification userInfo]];
currentFrame.origin.y = currentFrame.origin.y - change ;
self.view.frame = currentFrame;
}
最后,当键盘消失后,视图需要恢复原状。
-(void)keyboardWillDisappear:(NSNotification *)notification
{
CGRect currentFrame = self.view.frame;
CGFloat change = [self keyboardEndingFrameHeight:[notification userInfo]];
currentFrame.origin.y = currentFrame.origin.y + change ;
self.view.frame = currentFrame;
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
在viewDidLoad中添加如下代码:
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillAppear:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillDisappear:)
name:UIKeyboardWillHideNotification object:nil];
当系统消息出现UIKeyboardWillShowNotification和UIKeyboardWillHideNotification消息就会调用我们的keyboardWillAppear和keyboardWillDisappear方法。
其次键盘的高度计算:
-(CGFloat)keyboardEndingFrameHeight:(NSDictionary *)userInfo//计算键盘的高度
{
CGRect keyboardEndingUncorrectedFrame
= [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
CGRect keyboardEndingFrame
= [self.view convertRect:keyboardEndingUncorrectedFrame fromView:nil];
return keyboardEndingFrame.size.height;
}
传入的(NSDictionary *)userInfo用于存放键盘的各种信息,其中UIKeyboardFrameEndUserInfoKey对应的存放键盘的尺寸信息,以CGRect形式取出。
// 将rect由rect所在视图转换到
effa
目标视图view中,返回在目标视图view中的rect
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
// 将rect从view中转换到当前视图中,返回在当前视图中的rect
- (CGRect)convertRect:(CGRect)rect
fromView:(UIView *)view;
最终返回的是键盘在当前视图中的高度。
然后,根据键盘高度将当前视图向上滚动同样高度。
-(void)keyboardWillAppear:(NSNotification *)notification
{
CGRect currentFrame = self.view.frame;
CGFloat change = [self keyboardEndingFrameHeight:[notification userInfo]];
currentFrame.origin.y = currentFrame.origin.y - change ;
self.view.frame = currentFrame;
}
最后,当键盘消失后,视图需要恢复原状。
-(void)keyboardWillDisappear:(NSNotification *)notification
{
CGRect currentFrame = self.view.frame;
CGFloat change = [self keyboardEndingFrameHeight:[notification userInfo]];
currentFrame.origin.y = currentFrame.origin.y + change ;
self.view.frame = currentFrame;
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
相关文章推荐
- iOS键盘弹出 视图向上滚动键盘高度
- ios 键盘弹出 视图向上滚动键盘高度
- ios 弹出键盘 视图向上平移
- 键盘弹出,视图向上移,键盘高度遮住UITextField的解决方法
- iOS键盘高度,键盘弹出视图上移动
- iOS 输入框弹出键盘后改变tableView高度内容不被遮挡
- iOS中,textField弹出键盘时,让整个视图上移,键盘回收,视图收回
- [置顶] iOS出现键盘的同时视图向上移动的方法讲解
- iOS监听键盘弹出,获取键盘的高度
- IOS 实现键盘弹出视图上移功能
- iOS 监听键盘的弹出,收起,并且拿到键盘的高度
- iOS 键盘弹出时获取键盘的高度
- iOS键盘弹出高度以及动画时间获取
- iOS输入视图(如UITextView UITextField)点击时没有弹出键盘输入窗
- iOS开发----键盘弹出和隐藏时移动视图,防止被键盘挡住
- iOS横屏获取键盘高度为0、键盘侧面弹出问题
- 修复ios上第三方输入法弹出时输入键盘盖住网页没有进行相应滚动从而盖住表单输入框的问题
- 软键盘弹出,scrollview或listview向上滚动
- ios---实现随着键盘弹出,响应的视图也随之上移
- iOS 监听键盘弹出的高度,避免挡住界面