ios键盘弹起不遮挡输入框
2015-10-23 11:27
337 查看
- (void)viewDidAppear:(BOOL)animated{
[super
viewDidAppear:animated];
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(keyboardWillShown:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWillShown:(NSNotification *)aNotification {
BOOL isMessageEdit = [_inputTextField
isEditing];
if(isMessageEdit){
NSDictionary* info = [aNotification
userInfo];
CGSize keyboardSize = [[info
objectForKey:UIKeyboardFrameBeginUserInfoKey]
CGRectValue].size;
float time = [[info
objectForKey:UIKeyboardAnimationDurationUserInfoKey]
floatValue];
NSLog(@"keyboardSize.height is %f",keyboardSize.height);
[UIView
animateWithDuration:time animations:^{
CGRect frame = self.base_baseView.frame;
frame.origin.y -= keyboardSize.height;
self.base_baseView.frame = frame;
}];
}
}
- (void)keyboardWillBeHidden:(NSNotification *)aNotification{
NSDictionary* info = [aNotification
userInfo];
float time = [[info
objectForKey:UIKeyboardAnimationDurationUserInfoKey]
floatValue];
[UIView
animateWithDuration:time animations:^{
CGRect frame = self.base_baseView.frame;
frame.origin.y =
64;
self.base_baseView.frame = frame;
}];
}
其中BOOL isMessageEdit = [_inputTextField isEditing];是用来判断是不是_inputTextField这个textfield在编辑状态,然后利用 NSDictionary*
info = [aNotification userInfo];来获取键盘的一些信息,我将这个字典打印出来如下:
{
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {768, 318}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {384, 1183}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {384, 865}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 1024}, {768, 318}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 706}, {768, 318}}";
}
可以看到键盘的信息,比较常用的[b]UIKeyboardAnimationDurationUserInfoKey键盘弹起的时间,[b]UIKeyboardFrameBeginUserInfoKey可以获取键盘弹出的高度[/b][/b]
我是这样做的,想要避免_inputTextField被遮挡:
1.在进入页面之前注册一个通知,分别是键盘将要弹起和键盘将要收起的通知:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShown:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
2.判断是不是这个文本框在编辑:
BOOL isMessageEdit = [_inputTextField isEditing];
3.获取键盘弹出时间和弹出高度
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
float time = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
4.将view在获取的时间里弹起键盘的高度:
[UIView animateWithDuration:time animations:^{
CGRect frame = self.base_baseView.frame;
frame.origin.y -= keyboardSize.height;
self.base_baseView.frame = frame;
}];
5.在键盘将要收起的通知里,将页面恢复:
[UIView animateWithDuration:time animations:^{
CGRect frame = self.base_baseView.frame;
frame.origin.y = 64;
self.base_baseView.frame = frame;
}];
我这个带有导航栏,所以从64开始,你的如果没有可以设置y=0哦。
搞定!!
[super
viewDidAppear:animated];
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(keyboardWillShown:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWillShown:(NSNotification *)aNotification {
BOOL isMessageEdit = [_inputTextField
isEditing];
if(isMessageEdit){
NSDictionary* info = [aNotification
userInfo];
CGSize keyboardSize = [[info
objectForKey:UIKeyboardFrameBeginUserInfoKey]
CGRectValue].size;
float time = [[info
objectForKey:UIKeyboardAnimationDurationUserInfoKey]
floatValue];
NSLog(@"keyboardSize.height is %f",keyboardSize.height);
[UIView
animateWithDuration:time animations:^{
CGRect frame = self.base_baseView.frame;
frame.origin.y -= keyboardSize.height;
self.base_baseView.frame = frame;
}];
}
}
- (void)keyboardWillBeHidden:(NSNotification *)aNotification{
NSDictionary* info = [aNotification
userInfo];
float time = [[info
objectForKey:UIKeyboardAnimationDurationUserInfoKey]
floatValue];
[UIView
animateWithDuration:time animations:^{
CGRect frame = self.base_baseView.frame;
frame.origin.y =
64;
self.base_baseView.frame = frame;
}];
}
其中BOOL isMessageEdit = [_inputTextField isEditing];是用来判断是不是_inputTextField这个textfield在编辑状态,然后利用 NSDictionary*
info = [aNotification userInfo];来获取键盘的一些信息,我将这个字典打印出来如下:
{
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {768, 318}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {384, 1183}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {384, 865}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 1024}, {768, 318}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 706}, {768, 318}}";
}
可以看到键盘的信息,比较常用的[b]UIKeyboardAnimationDurationUserInfoKey键盘弹起的时间,[b]UIKeyboardFrameBeginUserInfoKey可以获取键盘弹出的高度[/b][/b]
我是这样做的,想要避免_inputTextField被遮挡:
1.在进入页面之前注册一个通知,分别是键盘将要弹起和键盘将要收起的通知:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShown:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
2.判断是不是这个文本框在编辑:
BOOL isMessageEdit = [_inputTextField isEditing];
3.获取键盘弹出时间和弹出高度
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
float time = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
4.将view在获取的时间里弹起键盘的高度:
[UIView animateWithDuration:time animations:^{
CGRect frame = self.base_baseView.frame;
frame.origin.y -= keyboardSize.height;
self.base_baseView.frame = frame;
}];
5.在键盘将要收起的通知里,将页面恢复:
[UIView animateWithDuration:time animations:^{
CGRect frame = self.base_baseView.frame;
frame.origin.y = 64;
self.base_baseView.frame = frame;
}];
我这个带有导航栏,所以从64开始,你的如果没有可以设置y=0哦。
搞定!!
相关文章推荐
- iOS激情详解之登录注册(XMPP)
- IOS--错误集锦--reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
- iOS页面切换动画实现方式。
- iOS客户端学习-ios9 urlError
- iOS巅峰之while与do...while详细区别
- 论ios代理和协议
- 举例讲解iOS开发中拖动视图的实现
- IOS入门篇(一)
- iOS开发全面解析多线程
- iOS开发-文件管理(一)
- IOS页面布局 纯代码
- iOS OC声明变量在@interface括号中与使用@property的区别
- iOS重绘机制drawRect 附:画图画线等code
- ios 使用pods
- IOS开发常用的正则表达式
- iOS常见错误
- 源码推荐(10.20):GitHub iPhone 客户端--MrCode,实时跟踪分析iOS App视图的小工具
- iOS处理分享时,在三方应用中无法返回自己应用的问题
- iOS中常见错误
- iOS实现一个颜色渐变的弧形进度条